Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 1st, 2006, 03:33 AM
handshakeit handshakeit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Location: Noida/India
Posts: 127 handshakeit User rank is Private First Class (20 - 50 Reputation Level)handshakeit User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 14 h 14 m 31 sec
Reputation Power: 8
Question which of these for loop styles is more efficient ( was "Plz Help...." )

Hi there,


Tell me which is more efficient of these two
FOR LOOPS

for(int i=0;i<10;i++);
or
for(int i=0;i<10;++i);

pls:-Justify your Answers.

Thanx
Devs

Reply With Quote
  #2  
Old February 1st, 2006, 04:41 AM
SimonGreenhill's Avatar
SimonGreenhill SimonGreenhill is offline
(retired)
Dev Shed God 11th Plane (10000 - 10499 posts)
 
Join Date: Dec 2003
Location: The Laboratory
Posts: 10,101 SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)  Folding Points: 4925 Folding Title: Novice Folder
Time spent in forums: 3 Months 3 Weeks 6 h 8 m 34 sec
Reputation Power: 0
Facebook
Hello and welcome to Devshed.

Please use an informative thread title in future. For example, in this case, something like "which of these for loops is more efficient" would be a good choice.

Second, what language are you looking at? I'm guessing this is highly language dependant.

Third, premature optimisation is the root of all evil. Something like this is going to provide extremely minor benefits, if any, in most cases.

Fourth - have you tried benchmarking it yourself?

--Simon

Reply With Quote
  #3  
Old February 1st, 2006, 08:08 AM
LinuxPenguin's Avatar
LinuxPenguin LinuxPenguin is offline
fork while true;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2005
Location: England, UK
Posts: 5,538 LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)LinuxPenguin User rank is General 1st Grade (Above 100000 Reputation Level)  Folding Points: 11590 Folding Title: Novice Folder
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 30 m 28 sec
Reputation Power: 1050
There should be no difference in a compiled language.

There should be very little (IF ANY) difference in an interpreted language and it would be impossible to tell which would be more efficient. Let the compiler optimise for you, you have to read the code easily, not the computer.

As said above, benchmark it yourself.
Comments on this post
Yawmark agrees!

Reply With Quote
  #4  
Old February 1st, 2006, 10:14 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 36 m 15 sec
Reputation Power: 4080
The ++i will be more efficient, but I won't specify the reason why for the next few days, because this question smells an awful lot like a cut-and-paste homework assignment. I'll also say that when using integer variables, it may not matter for good optimizing compilers because they'll probably optimize it the correct way anyway.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Reply With Quote
  #5  
Old February 7th, 2006, 03:27 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 36 m 15 sec
Reputation Power: 4080
Now that a week has passed since the last post, I'll answer this question now. The answer should be fairly obvious if you work with operator overloading. See the notes to overload ++ and -- in this tutorial

Note that for a post-increment operator, it returns a value instead of a reference
Quote:
As to why the function returns Complex instead of Complex &, this is fairly easy to explain: The post-increment operator is supposed to return the current value of the object and then increment it. Hence, we store the current value in a temporary variable, increment the members and then return the temporary variable (which holds the previous value of the object).

What this means is that, with ++i, it needs to
(1) find the object (i) in memory,
(2) increment it and
(3) return a reference to that object.

In case of i++, it needs to
(1) find the object,
(2) copy it to a temporary location,
(3) increment the original object and
(4) return the value from the temporary location.

The extra operation is in copying to a temporary location. This doesn't take much effort for an integer, but takes more for a larger class object.
Comments on this post
Axweildr agrees: patience is a virtue, as you grow older, you will find memory moreso ...
netytan agrees: The ever brilliant Scorpi everyone
medialint agrees!

Reply With Quote
  #6  
Old February 13th, 2006, 07:40 AM
Yawmark's Avatar
Yawmark Yawmark is offline
Feelin' Groovy
Dev Shed God 11th Plane (10000 - 10499 posts)
 
Join Date: Aug 2001
Location: WDSMIA
Posts: 10,135 Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 4 Days 1 h 22 m 45 sec
Reputation Power: 5052
Send a message via ICQ to Yawmark Send a message via MSN to Yawmark
Note that in Java (1.5), there is no difference in the bytecode:

Java Code:
Original - Java Code
  1. public void pre() {
  2.     for(int i = 0; i < 10; ++i);
  3. }
  4.  
  5. // public void pre();
  6. //   Code:
  7. //    0:    iconst_0
  8. //    1:    istore_1
  9. //    2:    iload_1
  10. //    3:    bipush  10
  11. //    5:    if_icmpge   14
  12. //    8:    iinc    1, 1
  13. //    11:   goto   2
  14. //    14:   return
  15.    
  16. public void post() {
  17.     for(int i = 0; i < 10; i++);
  18. }
  19.  
  20. // public void post();
  21. //   Code:
  22. //    0:    iconst_0
  23. //    1:    istore_1
  24. //    2:    iload_1
  25. //    3:    bipush  10
  26. //    5:    if_icmpge   14
  27. //    8:    iinc    1, 1
  28. //    11:   goto   2
  29. //    14:   return
  30.  
__________________
Yawmark
class Sig{public static void main(String...args){\u0066or(int
\u0020$:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray()
)System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
+(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

Reply With Quote
  #7  
Old February 14th, 2006, 05:11 PM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,907 LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 6 h 33 m 15 sec
Reputation Power: 583
Same in C#. When the value of the incrementer (-or?) is not used (like in for loops, and other times when the incrementation is the whole statement), then the IL is the same whether you use post- or pre-incrementation.

Code:
// C#
int i = 0;
i++; // or ++i, doesn't matter

// MSIL
// (Comments are explanatory, not actually part of MSIL)
.locals init ([0] int32 i)
IL_0000:  ldc.i4.0  // load value 0
IL_0001:  stloc.0  // store in location 0 (i)
IL_0002:  ldloc.0  // load location 0 (i)
IL_0003:  ldc.i4.1  // load value 1
IL_0004:  add
IL_0005:  stloc.0  // store in location 0


However, when you use the value of i, then things are only slightly different between post- and pre-incrementation.

Code:
givens
// C#
int a;
int i = 2;

// MSIL
.locals init ([0] int32 a, [1] int32 i)
IL_0000:  ldc.i4.2
IL_0001:  stloc.1


Code:
post-incrementation
// C#
a = i++; // a == 2, i == 3

// MSIL
IL_0002:  ldloc.1
IL_0003:  dup
IL_0004:  ldc.i4.1
IL_0005:  add
IL_0006:  stloc.1
IL_0007:  stloc.0


Code:
pre-incrementation
// C#
a = ++i; // i == 3, a == 3

// MSIL
IL_0002:  ldloc.1
IL_0003:  ldc.i4.1
IL_0004:  add
IL_0005:  dup
IL_0006:  stloc.1
IL_0007:  stloc.0


No much difference in the IL, just whether dup is used before or after the add.
__________________
Joel B Fant
"An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."

.NET Must-Haves
Tools: Reflector
References: Threading in .NET

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > which of these for loop styles is more efficient ( was "Plz Help...." )

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap