Improve code performance in C #

4

I'm doing some testing with some C # code and during an analysis I realized that some high peaking problems happen when whole-valued conversions to string occur, in most cases the conversion happens like this: classe.valor.ToString("0.0") is there any way to improve this type of conversion? Using% w / w% the peak was even higher by reducing application performance. Would you have an alternative?

These conversions happen within a continuous function, which is updated every frame .

You can not post the whole code here, the values come from several classes so I'll post only a few snippets of one of the classes, so I can apply it to the other ones:

distancia.text = string.Concat(string.Format("{0:0.00}", distance.metros), " m");

 if (contar)
    {
        i -= Time.deltaTime;

        timeCount.text = i.ToString("0").Replace("0","Go");

        if (i < 0)
        {                
            contar = false;
        }
    }

checkpoint.text = string.Concat("Distance: ", string.Format("{0:0.00}", distance.metros), " m");

if (manager.count)
    {
      var valor = int.Parse(rota.metros.ToString("00"));
      _menuManager.ShowMenu(Menu);
      endPoints.text = string.Concat("distance: ", string.Format("{0:0.00}", rotaMaior.metros), " m");
    }
    
asked by anonymous 31.07.2015 / 16:57

2 answers

4

Just looking at this stretch has nothing that can be done to get significantly better results. I could tell you to do a formatting function of your own, but it would probably be less efficient. I could tell you to take this out that is causing the problem, but you need it, you should not have used it idly.

It has a% awkward% there. It seems to be mixing UI with data processing. This should not happen. This is bad software engineering. I would find a way to separate things better. And it may be that this method is causing the problem. It seems absurdly heavier than the rest of the operation.

You have to try to isolate the problem somehow. See exactly where the problem is. You can not kick. Either make an on-site measurement or use a profiling tool like the one I indicated in that answer a>.

The _menuManager.ShowMenu(Menu) method is quite efficient. And even if it was not, it would not have problems in this case since the concatenation has few items.

This code may be being called more times than necessary.

Otherwise, only if it has other parts that might be causing it, maybe this is in a badly mounted loop.

    
31.07.2015 / 17:37
2

There are many things that can be done to make your code performance so much better.

However, there are two things you really should do and refactor in order to speed up performance:

  • Separate processing outputs: In your code the processing of your calculations with output is very mixed for your interface. I recommend removing everything and turning it into a real object, closed for modification but open for extension. Apparently you're using a Timer type, it should have an event like * TimeElapsed * or something. Use it to update your interface, so this processing will be in a separate thread , isolating the mainstream
  • Improve casting - type conversion - and string manipulation of your code. These conversions are costly, especially if executed in loop . And manipulating strings is also costly because they are immutable.
  • Some examples:

    //distancia.text = string.Concat(string.Format("{0:0.00}", distance.metros), "m");
    distancia.text = string.Format("{0:0.00} m", distance.metros); // Removido um Concat()
    
    //checkpoint.text = string.Concat("Distance: ", string.Format("{0:0.00}", distance.metros), " m");
    checkpoint.text = string.Format("Distance: {0:0.00} m", distance.metros); // Removido um Concat()
    
    //endPoints.text = string.Concat("distance: ", string.Format("{0:0.00}", rotaMaior.metros), " m");
    endPoints.text = string.Format("distance: {0:0.00} m", rotaMaior.metros); // Removido um Contac()
    

    Use ternary operators:

    //timeCount.text = i.ToString("0").Replace("0","Go");
    timeCount.text = i > 0 ? i : "Go"; // Removido ToString() e Replace()
    

    Casting Reduction:

    //var valor = int.Parse(rota.metros.ToString("00")); // Qual o tipo desse rota.metros ?
    var valor = rota.metros; //Não vi onde você usar esse 'valor'
    

    And I agree with @bigown, this _menuManager.ShowMenu(Menu) is strange in this place. If you can share more code, it will help us to help you more.

        
    26.04.2016 / 21:40