473,785 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Speed of do while

We can read this comment at php.net
jon
05-Nov-2003 10:06
According to my tests, the "do...while " control structure actually seems to be ~40% faster than the "for" control structure. At least using PHP v4.0.6on a few fairly modern Linux machines. I used a the getmicrotime function as defined at http://www.php.net/microtime
I tested the following 5 scenarios, including two different ways of calling a "for" loop. The less ordinary way to call "for", I'll call "altfor".
Here >are the code scenarios for different ways from counting from
0->($num-1), in my case $num = 10000:
// 'altfor':
$a=0;
for (; $a<$num;) { $a++;} // 'for':
for ($a=0; $a<$num; $a++) {} // 'foreach':
foreach (range(0,$num-1) as $a ) {} // 'while':
$a = 0;
while ($a < $num) {$a++;} // 'do':
$a = 0;
do {} while (++$a < $num); Each scenario is called in random order, and here is a typical result: Average times for 100 iterations of each loop: altfor, do, for, foreach, while.All loops are incremented from 0 to 9999. altfor: 0.0368828332424 16 seconds, 138.25 percent of ~ 0.0267 seconds
do: 0.0266782820224 76 seconds, 100.00 percent of ~ 0.0267 seconds
for: 0.0373089981079 10 seconds, 139.85 percent of ~ 0.0267 seconds
foreach: 0.0648571395874 02 seconds, 243.11 percent of ~ 0.0267 seconds
while: 0.0365088176727 29 seconds, 136.85 percent of ~ 0.0267 seconds

...so to be clear, I ran "for", et all, from 0->9999. This was done 100

times. Above are the average times for those 100 tests of loops of 10,000.
I >have run my script multiple time on several different Linux systems here
at my work. All w/ similar results...

As anyone tryed to see if thats true and do...while is about 40% faster than
for ?




Jul 17 '05 #1
2 2170
Just curious, but put some real calculations or procedures within the loops,
perhaps read a file, process an array or combo therof.

The reason is, it might not be 40% faster, but rather a small time frame of
less overhead. run some calculations that take 1 second to run, and put that
in the loop 10 times, thus roughly 10 seconds, then you might see some loops
running at 9.8 or 10.1, but not 10 sec and 6 sec (40% faster)

--
Mike Bradley
http://www.gzentools.com -- free online php tools

"Marco" <mpgtlatbluewin dotch> wrote in message
news:40******** **@news.bluewin .ch...
We can read this comment at php.net
jon
05-Nov-2003 10:06
According to my tests, the "do...while " control structure actually seems to
be ~40% faster than the "for" control structure. At least using PHP v4.0.6on a few fairly modern Linux machines.

I used a the getmicrotime function as defined at

http://www.php.net/microtime
I tested the following 5 scenarios, including two different ways of

calling a "for" loop. The less ordinary way to call "for", I'll call "altfor".
Here >are the code scenarios for different ways from counting from
0->($num-1), in my case $num = 10000:
// 'altfor':
$a=0;
for (; $a<$num;) { $a++;}
// 'for':
for ($a=0; $a<$num; $a++) {}

// 'foreach':
foreach (range(0,$num-1) as $a ) {}

// 'while':
$a = 0;
while ($a < $num) {$a++;}

// 'do':
$a = 0;
do {} while (++$a < $num);

Each scenario is called in random order, and here is a typical result:

Average times for 100 iterations of each loop: altfor, do, for, foreach,

while.
All loops are incremented from 0 to 9999.

altfor: 0.0368828332424 16 seconds, 138.25 percent of ~ 0.0267 seconds
do: 0.0266782820224 76 seconds, 100.00 percent of ~ 0.0267 seconds
for: 0.0373089981079 10 seconds, 139.85 percent of ~ 0.0267 seconds
foreach: 0.0648571395874 02 seconds, 243.11 percent of ~ 0.0267 seconds
while: 0.0365088176727 29 seconds, 136.85 percent of ~ 0.0267 seconds

...so to be clear, I ran "for", et all, from 0->9999. This was done 100

times. Above are the average times for those 100 tests of loops of

10,000. I >have run my script multiple time on several different Linux systems here at my work. All w/ similar results...

As anyone tryed to see if thats true and do...while is about 40% faster than for ?





Jul 17 '05 #2
This has been the case since PHP3. Does it matter? Probably not.

Functions with longer names are also slower, in case you're wondering. Dito
for long variable names.

Uzytkownik "Marco" <mpgtlatbluewin dotch> napisal w wiadomosci
news:40******** **@news.bluewin .ch...
We can read this comment at php.net
jon
05-Nov-2003 10:06
According to my tests, the "do...while " control structure actually seems to
be ~40% faster than the "for" control structure. At least using PHP v4.0.6on a few fairly modern Linux machines.

I used a the getmicrotime function as defined at

http://www.php.net/microtime
I tested the following 5 scenarios, including two different ways of

calling a "for" loop. The less ordinary way to call "for", I'll call "altfor".
Here >are the code scenarios for different ways from counting from
0->($num-1), in my case $num = 10000:
// 'altfor':
$a=0;
for (; $a<$num;) { $a++;}
// 'for':
for ($a=0; $a<$num; $a++) {}

// 'foreach':
foreach (range(0,$num-1) as $a ) {}

// 'while':
$a = 0;
while ($a < $num) {$a++;}

// 'do':
$a = 0;
do {} while (++$a < $num);

Each scenario is called in random order, and here is a typical result:

Average times for 100 iterations of each loop: altfor, do, for, foreach,

while.
All loops are incremented from 0 to 9999.

altfor: 0.0368828332424 16 seconds, 138.25 percent of ~ 0.0267 seconds
do: 0.0266782820224 76 seconds, 100.00 percent of ~ 0.0267 seconds
for: 0.0373089981079 10 seconds, 139.85 percent of ~ 0.0267 seconds
foreach: 0.0648571395874 02 seconds, 243.11 percent of ~ 0.0267 seconds
while: 0.0365088176727 29 seconds, 136.85 percent of ~ 0.0267 seconds

...so to be clear, I ran "for", et all, from 0->9999. This was done 100

times. Above are the average times for those 100 tests of loops of

10,000. I >have run my script multiple time on several different Linux systems here at my work. All w/ similar results...

As anyone tryed to see if thats true and do...while is about 40% faster than for ?





Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
2644
by: Mark Dickinson | last post by:
I have a simple 192-line Python script that begins with the line: dummy0 = 47 The script runs in less than 2.5 seconds. The variable dummy0 is never referenced again, directly or indirectly, by the rest of the script. Here's the surprise: if I remove or comment out this first line, the script takes more than 15 seconds to run. So it appears that adding a redundant line produces a spectacular six-fold increase in speed!
4
2082
by: Il Prof | last post by:
Hi to everyone! ;) In C language on Linux, what string access method is faster? 1) Puntatori char str="hello"; char* pstr; pstr = str; // access such as "*pstr"
4
2916
by: hamil | last post by:
I wrote a VB.net "echo" program using the Tcp client/listener class to determine how fast the transactions would occur. I got a speed of about 160 echoes per second. A friend did the same test using Linux/C++ and reported 3000 echoes/second. Is my number typical, or am I doing something wrong? How fast should I expect an echo test to run? Thanks, Hamil.
11
2001
by: Jim Lewis | last post by:
Has anyone found a good link on exactly how to speed up code using pyrex? I found various info but the focus is usually not on code speedup.
8
2457
by: mast2as | last post by:
I am sure this topic has been discussed a thousand times and I read a few things about it today on the net. I also want to say I am trying to start a polemic here, I am just curious and willint to learn and improve the way I am approaching some coding issues that I have at the moment. I use C++ programming for my work, but I am not a developper so please be patient & tolerant in your answers ;-) Okay for the last few days I have been...
0
1112
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
Being you can drag webparts from zone to zone, is there a way to control drag speed while dragging/scrolling up the page? I have a list of webparts that may go beyond the page. If a user wants to drag the bottom webpart up to the top, I was wondering if the speed of this drag/scroll action can be controlled. Thanks
1
1956
by: powerej | last post by:
program is suppose to ask for an input from 1-8, if the user enters a number outside of this range than it will continue to ask until it does, something is wrong and i cant seem to figure it out, i am stuck. Also my if else to see if the user catches the wave seems right to me but only works like half the time. I just really want to know what i am doing wrong here. import javax.swing.*; // gets option pane import...
11
6494
by: kyosohma | last post by:
Hi, We use a script here at work that runs whenever someone logs into their machine that logs various bits of information to a database. One of those bits is the CPU's model and speed. While this works in 95% of the time, we have some fringe cases where the only thing returned is the processor name. We use this data to help us decide which PCs need to be updated, so it would be nice to have the processor speed in all cases.
2
2896
by: alireza6485 | last post by:
Hi, Could you please rewrite the program for me?I tried my best and the program still does not do what it has to do. I have to write a code that generates random speed and distance .it ask the user for angle and start calculating the vertical and horizantal positions. when the vertical position gets negative program should stop and check the horizantal position and print out different messeges based on the value of the horizantal speed. ...
2
1294
by: Tony Koone | last post by:
import javax.swing.JOptionPane; public class DistanceTraveled { public static void main(String args) { int Distance; // D = Distance Traveled
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.