Register for your free account! | Forgot your password?

You last visited: Today at 22:45

  • 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 01/27/2013, 14:04   #2
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,124
Received Thanks: 2,502
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 01/27/2013, 19:06   #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 01/27/2013, 21:38   #4
 
elite*gold: 0
Join Date: May 2005
Posts: 1,892
Received Thanks: 913
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 01/27/2013, 22:39   #5
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,124
Received Thanks: 2,502
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 01/28/2013, 00:18   #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 01/28/2013, 11:58   #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



« A problem faces me and some friends | PLEASE HELP ME »

Similar Threads
quick question
04/01/2010 - Dekaron - 0 Replies
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 if ya do id apreciate the help =P
quick question
12/13/2009 - CO2 Programming - 5 Replies
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 features and I dont got the source...so quick question just to conferm.. the first packet...
Very quick question need quick answer!
10/26/2009 - Dekaron - 5 Replies
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 speedy... thansk!
Just a quick question.
06/06/2009 - CO2 Main - Discussions / Questions - 4 Replies
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 hack will be more important to me atm. If there is a working zoom thats safe anywhere could someone...
Quick Question
11/12/2006 - CO2 Main - Discussions / Questions - 5 Replies
Is there a macro that attacks 4 stakes in tg? With FB/SS X=Stake o=Char X X o X X



All times are GMT +2. The time now is 22:45.


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

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