Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #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: 3
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 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)  Folding Points: 4925 Folding Title: Novice Folder
Time spent in forums: 3 Months 3 Weeks 5 h 41 m 45 sec
Reputation Power: 1331
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,535 LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)  Folding Points: 11590 Folding Title: Novice Folder
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 23 m 58 sec
Reputation Power: 999
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 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,336 Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 4 Weeks 12 h 46 m 43 sec
Reputation Power: 674
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

Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month

Reply With Quote
  #5  
Old February 7th, 2006, 03:27 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,336 Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 4 Weeks 12 h 46 m 43 sec
Reputation Power: 674
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
Click here for more information.
 
Join Date: Aug 2001
Location: WDSMIA
Posts: 7,404 Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level)Yawmark User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Week 6 Days 15 h 35 m 51 sec
Reputation Power: 1245
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
Barely Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,418 LyonHaert User rank is Captain (20000 - 30000 Reputation Level)LyonHaert User rank is Captain (20000 - 30000 Reputation Level)LyonHaert User rank is Captain (20000 - 30000 Reputation Level)LyonHaert User rank is Captain (20000 - 30000 Reputation Level)LyonHaert User rank is Captain (20000 - 30000 Reputation Level)LyonHaert User rank is Captain (20000 - 30000 Reputation Level)LyonHaert User rank is Captain (20000 - 30000 Reputation Level)LyonHaert User rank is Captain (20000 - 30000 Reputation Level)LyonHaert User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 3 Weeks 6 Days 9 h 40 m 35 sec
Reputation Power: 276
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 - LyonHaert.net

2 + 2 is... 10... in base 4

Reply With Quote
Reply

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


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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