Go Back   kirupaForum > Flash > ActionScript 3

Reply
 
Thread Tools Display Modes
Old 04-13-2008, 03:27 AM   #1
spiderbox.am
Registered User
++I vs I++

Hi everyone.
I just want to ask one question about the following:

//Version 1.
for(var i:uint = 0; i < 100; ++i)
trace("i = " + i);

//Version 2.
for(var i:uint = 0; i < 100; i++)
trace("i = " + i);

Which version is more correct ?

P.S.
I know the answer, just want to share
spiderbox.am is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?

Old 04-13-2008, 12:03 PM   #2
nikefido
so confused
i've always seen i++, so that's what I use.

is one more efficient?
nikefido is offline   Reply With Quote
Old 04-13-2008, 12:50 PM   #3
Voetsjoeba
Tu n'es pas un robot!
 
Voetsjoeba's Avatar
Location Belgium, Ghent

Posts 11,559
There is no "more correct" version in this case. There is a significant difference between i++ and ++i though. i++ will return the value of i and then increment it, while ++i will first increment i and then return its value.

*Wait, if you know the answer, why are you asking us instead of just telling people?

__________________
Wait, what?
Voetsjoeba is offline   Reply With Quote
Old 04-13-2008, 02:36 PM   #4
spiderbox.am
Registered User
Answer

Well, I just wanted to see opinions and some logic...
Yes, one of this versions is more efficient.

Lets see what is translator doing.
For executing statement (i++) it must do the following steps:

1. COPY the value of variable i to somewhere (for example the reference of this value will be j )

2. Increment the value of variable i and return j.

For executing statement (++i) it must do the following step:
1. Increment the value of variable i and return i !!!.

So NEVER use code like this:
for(var i:uint; i < 100; i++) ...

You are not solving BIG efficienty problem but why i++ instead of ++i.

Have a good day.
spiderbox.am is offline   Reply With Quote
Old 04-13-2008, 02:52 PM   #5
spiderbox.am
Registered User
Finally

FINALLY we have:

Its more efficient to do ++i, because i++ requires storing an intermediate value, incrementing, and then returning. For good practice, you should do ++i whenever you can, unless your real intended meaning is i++. However, modern compilers will do optimizations that will filter-out useless i++ statements and turn them into the equivalent of ++i.
spiderbox.am is offline   Reply With Quote
Old 04-14-2008, 12:58 AM   #6
Rezmason
Moyogi
 
Rezmason's Avatar
:-P The efficiency of a unary operator should have little influence on its "properness".

I would say i++ is the traditional, classic increment operator in for loops. Using ++i in that context is like making a fashion statement.

__________________
Wireworld player
Rezmason is offline   Reply With Quote
Old 04-14-2008, 01:42 AM   #7
Krilnon
≈ ≠ =
 
Krilnon's Avatar
Location Rochester, NY

Posts 7,697
I guess the ironic part of this thread is that the one talking about efficiency is the one using uints. (unless uint operations have become much faster recently, then all of the irony would be in my post )
Krilnon is online now   Reply With Quote
Old 04-14-2008, 04:27 PM   #8
spiderbox.am
Registered User
Yes, i++ is traditional for loops, but I think the reason is the following:
if you write for(var i:uint=0; i < 100; i+) compiler will give syntax error, on the other hand it will accept the second version without giving any warnings(for(var i:uint=0; i < 100; +i)) LOL
spiderbox.am is offline   Reply With Quote
Old 04-14-2008, 04:36 PM   #9
wvxvw
Holosuite User
Actually, the fastest way to do it is i+=1 %) (assuming i is int). But, in 99% of all loops you'll be optimizing - this is the last subject to change the difference isn't really worth it
wvxvw is offline   Reply With Quote
Old 04-14-2008, 04:39 PM   #10
spiderbox.am
Registered User
Quote:
Originally Posted by wvxvw View Post
Actually, the fastest way to do it is i+=1 %) (assuming i is int). But, in 99% of all loops you'll be optimizing - this is the last subject to change the difference isn't really worth it
Why i += 1 will work faster than i++ ???
spiderbox.am is offline   Reply With Quote
Old 04-14-2008, 04:47 PM   #11
wvxvw
Holosuite User
http://osflash.org/as3_speed_optimizations
Here're few good suggestions on optimization.

And here're few more
http://www.rockonflash.com/blog/?p=63

Last edited by wvxvw; 04-14-2008 at 04:51 PM..
wvxvw is offline   Reply With Quote
Old 04-14-2008, 04:58 PM   #12
spiderbox.am
Registered User
Well, I just got AMAZING results for loop operator.


var time1:Number = (new Date()).getTime();

for(var i:uint = 0; i < 100000000; ++i);

var time2:Number = (new Date()).getTime();

trace(time2-time1)

This code outputs ~1115 milliseconds.

BUT LETS SEE WHAT HAPPENED WHEN I CHOSE I+=1

var time1:Number = (new Date()).getTime();

for(var i:uint = 0; i < 100000000; i+=1);

var time2:Number = (new Date()).getTime();

trace(time2-time1)

RESULT IS AMAAAAAZING ~235 milliseconds!!!!!!!

P.S.
About my PC:
Intel Core 2 Duo E6700 , 3Gb RAM

Last edited by spiderbox.am; 04-14-2008 at 05:01 PM..
spiderbox.am is offline   Reply With Quote
Old 04-14-2008, 06:13 PM   #13
Jerryscript
Registered User
Location Las Vegas

Posts 820
Even faster I believe is:
PHP Code:
var time1:Number = (new Date()).getTime();

for(var 
i:uint 1000000000; --i);

var 
time2:Number = (new Date()).getTime();

trace(time2 time1); 
And I think fastest is:
PHP Code:
var time1:Number = (new Date()).getTime();

var 
i:uint 100000000;

while(
0) { --i;}

var 
time1:Number = (new Date()).getTime();

trace(time2 time1); 
Do a search for actionscript optimization and you'll find several threads and blog posts on this subject.

__________________
JerryScript
http://jerryscript.hostrocket.com
Events Calendar | Samples
Jerryscript is offline   Reply With Quote
Old 04-15-2008, 12:48 AM   #14
wvxvw
Holosuite User
Nope this was true for AS2, but in AS3 they made some optimizations for for loops, look at the first link from my previous post. Now while is slowlier than for if for increments (not decrements).
wvxvw is offline   Reply With Quote
Old 04-15-2008, 03:37 AM   #15
spiderbox.am
Registered User
Quote:
Originally Posted by Jerryscript View Post
Even faster I believe is:
PHP Code:
var time1:Number = (new Date()).getTime();

for(var 
i:uint 1000000000; --i);

var 
time2:Number = (new Date()).getTime();

trace(time2 time1); 
And I think fastest is:
PHP Code:
var time1:Number = (new Date()).getTime();

var 
i:uint 100000000;

while(
0) { --i;}

var 
time1:Number = (new Date()).getTime();

trace(time2 time1); 
Do a search for actionscript optimization and you'll find several threads and blog posts on this subject.

Hey, before posting your ideas you can test it.
I have just checked and both versions took ~1300 milliseconds.

So version with i+=1 ~235 is AMAAAAAZING !!!
spiderbox.am is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:26 PM.

SHARE:

SUPPORTERS:

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com