Pragmatic
Pragmatic

Reputation: 3127

Why infinity loop does not take cpu usage to 100

When I run a simple c# program with a empty while loop, my cpu usage goes up from 8-12% to 24-20% and core usage pattern is almost identical.

    static void Main(string[] args)
    {
        bool flag = true;
        while(flag)
        {

        }
    }

Could anyone please explain this behavior. I was expecting one core usage goes to 100% at this time.

Update: I am using octa-core machine and total cpu usage (summing up all core usage) goes to 25%. And each core (leaving the first one) shows similar usage pattern (which is around 25%)

enter image description here

Upvotes: 4

Views: 798

Answers (2)

Alexandre Nourissier
Alexandre Nourissier

Reputation: 338

As your application is monothreaded, you will only use one core. If it tops at 25%, I can guess the running environment is equipped with a quad-code processor.

Upvotes: 2

Tim
Tim

Reputation: 2912

Because it is single threaded. You probably have a Quad core CPU. You are likely maxing out the single core.

To fully max out modern multi-core CPUs you would need a multithreaded workload. In addition you would have to avoid optimizations if you get too cute with your code.

Upvotes: 3

Related Questions