Do these methods optimize cpu utilization?

Discussion and feedback on Construct 2

Post » Sun Jul 31, 2016 9:49 am

Well, something that my professor said while I was coding "C" at class made me think I might be doing many bad coding habits in Construct 2.
I wanted to know whether these bad habits of mine do significant impact on my game's performance.
My game is a rpg type of game & any performance gain means a lot to me since the mechanics of the game is quite big, if you are wondering why I bothered to ask this.


1)One thing that my professor told me about my bad habits is that I use 2 or more loops when I can just make a nested loop out of all of those.

For example (In C2's event system):

My way:

*For Each AI_Characters:
, ------> AI Behaviors....

*For Each AI_Characters:
, -------> AI Animations...


Professor's Way:

*For Each AI_Characters:
, --------> AI Behaviors
, ---------> AI Animations

I do that style because it looks neat on the eventsheet, also that the events are so long that I need to organize & especially that I am grouping it to avoid confusion.
So does separating conditions doubles the cpu computation & affect performance?


2) Also another thing, it is about the Else... In C2, I use the "invert condition" more times the "else condition" since I am not sure if the "else" will always work.
So in class, I did the same & my professor said that it will slow down the CPU...
At that moment, I thought about my game and concluded my game is wasting too much CPU... since I was using "Invert condition" more times than the "else condition".
So does it still matter in C2? Does it significantly affect performance?

My way:

*If Variable = 1
, move()

*If Variable is not = 1
, stop()


Professor's Way:

*If Variable = 1
, move()

*Else
, stop




Thanks for giving time answering my questions. :D
The Things you can create is only limited by your imagination. If you don't have the skills then use your motivation as a natural force to exceed all expectations. Chadori RebornXD
B
65
S
21
G
90
Posts: 1,124
Reputation: 59,862

Post » Sun Jul 31, 2016 10:35 am

I am not a coder, but on point 2, I think else is often misunderstood or misused and perceived not to work (maybe I now prove myself guilty). In c2 i'd use else when appropriate and assume that is more efficient. So in your example, if more than one object has the variable as an instant and you use else, the else line will only run if the condition fails for all objects. Not using else, but an invert or different number means all objects respond appropriately. Caveat: I did say I wasn't a coder.
B
18
S
12
G
1
Posts: 45
Reputation: 2,340

Post » Sun Jul 31, 2016 12:49 pm

I see some logic in the loop example, but I don't think using inverted events instead of "else" will have any noticable effect on the CPU usage. We have extremely fast processors in our devices now.
B
135
S
33
G
17
Posts: 1,557
Reputation: 20,721

Post » Sun Jul 31, 2016 5:44 pm

I'd say Yes on both cases.
Using two loops will be slower than one, because it has to run double the amount.
Else will be quicker because it doesn't have to check any variables, etc.
But Else isn't always quicker in c2 because it could act like an Else If, and have other conditions included.
B
47
S
22
G
65
Posts: 1,127
Reputation: 38,395

Post » Tue Aug 02, 2016 1:53 pm

Thanks Guys. I think I'll just rewrite my events for the loops but I don't think I'll change the inverts to else, instead I will just add an "else" with the invert to make an "else if". I hope that makes it a little optimized.
The Things you can create is only limited by your imagination. If you don't have the skills then use your motivation as a natural force to exceed all expectations. Chadori RebornXD
B
65
S
21
G
90
Posts: 1,124
Reputation: 59,862

Post » Tue Aug 02, 2016 2:02 pm

The golden answer to any performance question: measure it
If you can't measure a performance difference you're just wasting your time.

Having said that:
Re 1): loops have a certain amount of overhead, so combining both in to one loop removes the overhead of the second loop. Often this is negligible though. However these examples are not identical in what they do. If "AI animation" requires *all* "AI behavior" to have completed first, then you need two loops and moving it in to one will break it. You can only make that change if "AI animation" only depends on its own "AI behavior" (which in this case sounds reasonable). But you can't always just blindly make this change.

Re 2): in Construct 2, 'Else' is not the same as an inverted condition of the same type. In C2 'Else' means "if the previous event did not run", and it does not pick any instances. Inverting the condition will re-test every instance and run with the instances that meet the inverted condition. So again you cannot blindly swap one for the other.

In programming languages "Else" is a code block that is jumped to if the condition was not true, which happens without testing a second condition at all, so is faster if the condition is slow to test.

Again though I must emphasise, there may be something in your project using 50% of the CPU time, and worrying about details like this is trying to shave off 0.01% of the CPU time instead of focusing on the 50% that matters. This is why you must make measurements otherwise it's very likely you are simply wasting your time.
Scirra Founder
B
399
S
236
G
89
Posts: 24,519
Reputation: 195,351

Post » Tue Aug 02, 2016 2:34 pm

Yeah I totally agree measuring is very essential but the thing is that I don't even see any performance difference since in my PC, it's always near or exactly 60fps (Not sure on mobile though).
Well, I wouldn't say that it's a waste of time though since about 75 % or more of my post here in the forum always pays off. I get answers that I wouldn't be able to come up faster by self measurement.
And I did measure it and again no difference but I still want to know C2's event-making standards. The Do's & Don'ts that aren't written in the manual.
The Things you can create is only limited by your imagination. If you don't have the skills then use your motivation as a natural force to exceed all expectations. Chadori RebornXD
B
65
S
21
G
90
Posts: 1,124
Reputation: 59,862


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 6 guests