How to make my program consume less CPU without disturbing its execution?

Asked

Viewed 634 times

4

I have a program that reads the memory of a computer process continuously (with the while (true)), but it ends up requiring a lot of CPU, arriving at 20% used, my question is, how to decrease CPU usage without losing performance in the program?

I tried the Thread.Sleep(10), has worked, CPU usage has dropped to 1%, but even though it is only 10 thousandths, program performance has dropped dramatically.

while (Vars.GlowActive) //Essa variavel sempre vai ser true
    if (Vars.GlowAlways || Vars.GetKeyState(Vars.GlowButton) == 0)
    {
        for (int num = 1; num < 24; num++)
        {
            int entity = mem.Read<int>(Vars.bClient + Vars.EntList + (num * 0x10));
            ehealth = mem.Read<int>(entity + Vars.Health);

            int Glow = mem.Read<int>(entity + Vars.GlowIndex);
            int EntTeam = mem.Read<int>(entity + Vars.Team);
            if (EntTeam == Vars.MyTeam)
            {
                if (Vars.glowteamenabled)
                {
                    if (Vars.TeamRainbow)
                        TeamColor();
                    color[0] = Vars.glow_team_r / 255;
                    color[1] = Vars.glow_team_g / 255;
                    color[2] = Vars.glow_team_b / 255;
                    DrawGlow(Glow, color);
                }
            }
            else
            {
                if (Vars.glowenemyenabled)
                {
                    if (Vars.EnemyRainbow)
                        EnemyColor();
                    else if (Vars.glowhealth)
                    {
                        Vars.glow_enemy_r = 255 - 2.55f * ehealth;
                        Vars.glow_enemy_g = 2.55f * ehealth;
                        Vars.glow_enemy_b = 0;
                    }
                    color[0] = Vars.glow_enemy_r / 255;
                    color[1] = Vars.glow_enemy_g / 255;
                    color[2] = Vars.glow_enemy_b / 255;
                    DrawGlow(Glow, color);
                }
            }
        }
    }
    else if (one && Vars.BeepEnable)
    {
        Console.Beep(3000, 200);
        one = !one;
    }
}

Sumario:

mem.Read reads the memory of a process.

DrawGlow() writes in memory.

  • Instead of while couldn’t you use an event routine? Or even a timer: https://msdn.microsoft.com/pt-br/library/system.timers.timer.elapsed(v=vs.110). aspx do not understand much of C#, anyway I believe it was not to fall performance when using Thread.Sleep, may be some "conflict".

  • How does an event routine work? Never heard of it...

  • Maybe I didn’t choose the best words, for example using Ontimedevent to run in parallel. I still believe that it was not to have fallen the performance just because of a Sleep. That Thread may be running multiple times? Or something? Maybe it is possible to solve in your code and the problem is not even Sleep.

  • No, it runs only once, but it is inside a while true. I will put the code in the question.

  • You settled with the System.Timers.Timer? I almost got xD right

  • 1

    @Yes, it was close. = D

Show 1 more comment

3 answers

2


Try decreasing Sleep duration

Thread.Sleep(1);

Or use a Timer

var timer =  new  System.Timers.Timer();
timer.Interval = 10;
timer.Elapsed += (ctx, arg) => /*chama a sua funcao aqui*/;
timer.AutoReset = true;
timer.Start();

1

Not everything or anything

A program that runs without stopping consumes more CPU. A program that sleeps always loses performance. How about then do both, but not always?

        while ( Vars.GlowActive )
        {
            if ( Vars.GlowAlways || Vars.GetKeyState( Vars.GlowButton ) == 0 )
            {
                for ( int num = 1 ; num < 24 ; num++ ) // Ver observação
                {
                }

                // Já que teve uma execuçao válida, não cairia no elseif abaixo
                // então explicitamente tenta executar de novo
                continue;
            }
            else if ( one && Vars.BeepEnable )
            {
                Console.Beep( 3000 , 200 );
                one = !one;
            }
            // Se chegou aqui, não tinha dados a processar.
            // Dar uma folga a espera de novos dados.
            System.Threading.Thread.Yield();
        }

Observing

  • This number 24 looks kind of magical. See if it’s required by the API you’re using. But if it’s just an attempt to balance load between rounds and beeps, the best thing would be to take that for, and move the beep test inside the while.
  • It seems interesting the use of System.Threading.Thread.Yield.

-1

Decrease the amount of If s in your program, so I saw you can use an and in some if’s, this should improve a little.

  • 1

    Thanks for the tip, I believe that this will improve a little, but not enough, since the fact that the program overload a lot is because it is inside a while true.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.