Search Unity

How to use ProfileMarker in a multi-threaded environment?

Discussion in 'Scripting' started by MNNoxMortem, Jun 17, 2020.

  1. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    Code (CSharp):
    1. Parallel.ForEach(Partitioner.Create(0, 1000),
    2. range =>
    3.     {
    4.      var (from, to) = range;
    5.      for (var i = from; i <= to; i++){
    6.         profilerMarker.Begin();
    7.         MethodToMeasure();
    8.         profilerMarker.End();
    9.     }
    10. });
    I thought of

    Code (CSharp):
    1.  
    2. outerProfilerMarker.Begin();
    3. Parallel.ForEach(Partitioner.Create(0, 1000),
    4. range =>
    5.     {
    6.      var (from, to) = range;
    7.      for (var i = from; i <= to; i++){
    8.         var profilerMarker.= new ProfilerMarker("X");
    9.         profilerMarker.Begin();
    10.         MethodToMeasure();
    11.         profilerMarker.End();
    12.     }
    13. });
    14. outerProfilerMarker.End();
    15.  
    But creating markers within profiled scopes should be avoided if I understood it correctly (as their creation is while cheap obviously not free and can distort the measurement)
     
  2. MarcGFJ

    MarcGFJ

    Joined:
    May 16, 2019
    Posts:
    24
    I am also curious about this.