Exclude colliders from raycast

What do I need is to ignore some colliders during raycast (objects which I want to ignore is on the same layer with objects which I want to hit. For example, I have AI which uses colliders to detect enemies, but I don’t want its own collider to block the view.)

So, in general I need to use Physics.RaycastAll, exclude collider which I want to ignore and be happy, but it seems to work very slowly. So, what I have tested that is other ways to ignore colliders with using simple Physics.Raycast:

  1. Just disable colliders, then enable.

  2. Disable game objects, then enable.

  3. Change game object layer, then revert.

And here results for 1000k raycasts:
alt text

Code which I used - using System.Collections.Generic;using System.Diagnostics;using UnityEngine; - Pastebin.com

So, changing layers and then rolling back seems to be the most optimal solution. So, the question - is there any better solution?

Put the enemies on their own layer and Raycast to only that layer. It is also worth noting that Raycasts that start inside a collider, will ignore said collider.