Profiler.BeginSample() performance - does it affect it while not profiling?


If I will cover my code around with Profiler.BeginSample(methodname) and of course EndSample will it decrease my code performance while having a Developement build but not profiling it?

Of course it always has some overhead. However it shouldn’t be too much to worry about. Though you should make sure to not generate strings dynamically as this would produce additional garbage. Anyways profiling should be added temporarily when you actually debug / profile your game. So if you plan to keep the profiling code in your scripts you may wrap them with preprocessor tags.

Yes, it does affect performance, also when not profiling (though only in Development Builds, the call is conditionally compiled out in release builds), if a bit less as no data is recorded. It is the more costly way to add instrumentation to your profiler data and the recommended way is to use ProfilerMarker API instead. Profiler.BeginSample() needs to marshall the string it is passed to native and add it to the Profiler data stream. ProfilerMarker sends the string to the native backend on creation, obtaining a marker ID and forthwith only acts on an IntPtr that adds that marker ID to the data stream, resulting in reduced overhead.

ProfilerMarker.Begin and ProfilerMarker.End are also compiled out in Release builds. ProfilerMarker.Auto is not compiled out but just returns null in Release builds, making it negligible as well.