Register for your free account! | Forgot your password?

You last visited: Today at 05:24

  • Please register to post and access all features, it's quick, easy and FREE!

 

Mind = Blown. Quick Question

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Jul 2011
Posts: 204
Received Thanks: 27
Mind = Blown. Quick Question

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.



12k is offline  
Old   #2
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,227
Received Thanks: 2,476
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.


Korvacs is offline  
Thanks
1 User
Old   #3
 
elite*gold: 10
Join Date: May 2008
Posts: 292
Received Thanks: 199
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
Mr.Human is offline  
Old   #4
 
elite*gold: 0
Join Date: May 2005
Posts: 1,892
Received Thanks: 912
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).


Lateralus is offline  
Old   #5
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,227
Received Thanks: 2,476
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.
Korvacs is offline  
Old   #6
 
elite*gold: 0
Join Date: Jul 2011
Posts: 204
Received Thanks: 27
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.
12k is offline  
Old   #7
 
elite*gold: 0
Join Date: Apr 2008
Posts: 752
Received Thanks: 276
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.


Mr_PoP is offline  
Reply



« Previous Thread | Next Thread »

Similar Threads
quick question
ok just finally today updated this a6 and everytime i run the game it says dekaron exe has stopped working im usin windows vista ..any1 kno wats up...
0 Replies - Dekaron
quick question
I broke my wrist last week,so I got A few weeks of to play with getting myown proxy working.Ive been using A private one but it is lacking A few...
5 Replies - CO2 Programming
Very quick question need quick answer!
On line 21 of the CE non agro, what is the regular speed of the mobs? what do I change?? I wanna non agro that doesnt make the monsters so...
5 Replies - Dekaron
Just a quick question.
Ive been on these forums for a few days now and was wondering is there any Auto nado or zoom hacks? I cant seem to find any working ones, the zoom...
4 Replies - CO2 Main - Discussions / Questions
Quick Question
Is there a macro that attacks 4 stakes in tg? With FB/SS X=Stake o=Char X X o X X
5 Replies - CO2 Main - Discussions / Questions



All times are GMT +1. The time now is 05:24.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Abuse
Copyright ©2017 elitepvpers All Rights Reserved.