Mind = Blown. Quick Question

01/27/2013 13:05 12k#1
The difference between the 2 sections of code is causing a 50% CPU usage difference, im just wondering how, and why o.o.

0% CPU Usage:
Code:
            uint CurrentTime = Network.Imports.timeGetTime();
            while (Running)
            {
                CurrentTime = Network.Imports.timeGetTime();
                if (Players.Count > 0)
                {
                    Mobs.Monster[] _mobz = Monsters.Values;
                    foreach (Mobs.Monster Mob in _mobz)
                        Mob.Step(_mobz, CurrentTime);
                    Thread.Sleep(Settings.MobRestTime); //1500
                }
                else
                    Thread.Sleep(5000);
            }
50% CPU Usage:
Code:
            while (Running)
            {
                uint CurrentTime = Network.Imports.timeGetTime();
                if (Players.Count > 0)
                {
                    Mobs.Monster[] _mobz = Monsters.Values;
                    foreach (Mobs.Monster Mob in _mobz)
                        Mob.Step(_mobz, CurrentTime);
                    Thread.Sleep(Settings.MobRestTime); //1500
                }
                else
                    Thread.Sleep(5000);
            }
Literally the only difference is whether or not im constructing the variable inside or outside the loop. Would really love some insight on this.
01/27/2013 14:04 Korvacs#2
Theres actually little to no difference between the two examples as far as the compiler is concerned, that 50% spike should not be caused by the way you declare CurrentTime.
01/27/2013 19:06 Mr.Human#3
honestly in my opinion it would be that top loop sets the current time once then the bottem loop sets the time every time the loop resets resulting into importing the time multiple times which is causes the extra cpu usage but im fairly new to c++ so i would probably trust Korvacs opinion more
01/27/2013 21:38 Lateralus#4
Yeah, Korvacs is right. Also, I'd use Environment.TickCount rather than importing timeGetTime, as they do the exact same thing (at least all that you'll need throughout your source).
01/27/2013 22:39 Korvacs#5
Quote:
Originally Posted by *scott^kurack* View Post
honestly in my opinion it would be that top loop sets the current time once then the bottem loop sets the time every time the loop resets resulting into importing the time multiple times which is causes the extra cpu usage but im fairly new to c++ so i would probably trust Korvacs opinion more
No, they both set the CurrentTime variable within the loop, the top one just allocates it beforehand.
01/28/2013 00:18 12k#6
Quote:
Originally Posted by Korvacs View Post
Theres actually little to no difference between the two examples as far as the compiler is concerned, that 50% spike should not be caused by the way you declare CurrentTime.
yeah, this was my thought exactly, thats why my mind is like blown haha. It literally changes the CPU usage by 50%. No idea why.

@Lateralus Thanks for the advice. It seems as if i was told that timegettime never went negative whereas tickcount did. So was better to use for general purpose. Now that ive done more reading, makes more sense to go with tickcount.
01/28/2013 11:58 Mr_PoP#7
try to not use P/Invokes as possible as you can!

because :-

1-Marshalling between managed/unmanaged types has an additional overhead
2-Doesn't work in medium trust
3-Not quite intuitive and could lead to subtle bugs like leaking handles, corrupting memory, ...
4-It could take some time before getting it right when looking at an exported C function
5-Problems when migrating from x86 to x64
6-When something goes wrong you can't simply step into the unmanaged code to debug and understand why you are getting exceptions. You can't even open it in Reflector :-)
7-Restricts cross platform interoperability (ie: you can't run under Linux if you rely on a Windows-only library).

Conclusion: use only if there's no managed alternative or in performance critical applications where only unmanaged libraries exist to provide the required speed.