Here is the java code too. First maxval1.java:
public class maxval1 {
static int $i, $x;
public static void main(String args[]) throws Exception {
for ($x=$i=1; $i<100000000/10; $i++) {$x++;}
System.out.println($x);
}
}
And maxval2.java: public class maxval2 {
static int $a, $i, $x;
public static void main(String args[]) throws Exception {
$a=100000000/10;
for ($x=$i=1; $i<$a; $i++) {$x++;}
System.out.println($x);
}
}
Does anyone have any ideas for what's happening in the Python example? Could you post the Python code?
Sure... here is maxval1.py:
for i in range(1, 100000000/10):
pass
And here is maxval2.py: maxval=100000000/10
for i in range(1, maxval):
pass
Oh, of course - range() is evaluated once in either case. It returns an array, which is then stepped through. The time difference must have been just some inaccuracy. I guess 'time' isn't the best command to use to test efficiency ... or maybe try adding another 0?
Oh, thanks. I redid it using xrange instead of range and the numbers went down to an average (over 20 tries) of about 1.36 sec for maxval1 (which computes endpoint within the xrange) and 1.9 sec for maxval2 (which uses variable in xrange.)
mbp:~/test doug$ time php -r 'for ($i=1; $i<100000000/10; $i++) {$x++;};'
The results are a little different with Python (maxval1.py computes inline; maxval2 divides in advance and uses variable inline) Java shows even more of a difference (again, maxval2 sets variable in advance):