Re: Wierd ass code...

From: Date: Mon, 15 May 2006 08:44:13 +0000
Subject: Re: Wierd ass code...
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-236177@lists.php.net to get a copy of this message
Gonzalo Monzón escribió:
Attached results and code. (tested on PHP 4.3.4) -------------------------------------------------------------- Testing bidimensional and single dimension array manipulation Fill style A, bidimensional array: 2,1603 Iteration style A, bidimensional array: 9,9071 Total time style A: 12,0681 Fill style B, single-dimension array: 4,2362 Iteration style B, single-dimension array: 1,5452 Total time style B: 5,7821 Fill style A, bidimensional array: 2,3548 Iteration style A, bidimensional array: 13,1935 Total time style A: 15,5489 Fill style B, single-dimension array: 4,2891 Iteration style B, single-dimension array: 1,5413 Total time style B: 5,8312 Fill style A, bidimensional array: 2,36 Iteration style A, bidimensional array: 17,8439 Total time style A: 20,2046 Fill style B, single-dimension array: 4,2982 Iteration style B, single-dimension array: 1,5554 Total time style B: 5,8544 Fill style A, bidimensional array: 2,3756 Iteration style A, bidimensional array: 17,8817 Total time style A: 20,2579 Fill style B, single-dimension array: 4,3203 Iteration style B, single-dimension array: 1,5332 Total time style B: 5,8543 Fill style A, bidimensional array: 2,3554 Iteration style A, bidimensional array: 19,2986 Total time style A: 21,6547 Fill style B, single-dimension array: 4,3013 Iteration style B, single-dimension array: 1,5508 Total time style B: 5,8528 I wonder how does PHP4 performs in this test, comparing with Python when using dictionaries.
Python wins! Please, don't flame this as I don't want to mean PHP is bad, they are different languages for different approaches. But these are the results for this test. Interesting... Best case A, bi-dimensional: PHP 12,0681 Python: 1,9930 (n/6,3) Best case B, single-dimension: PHP 5,7821 Python: 3,6950 (n/1,56) Python test, results & code (tested on Python 2.4.3): ------------------------------------------------------------------- Fill style A, bidimensional array: 1.742000 Iteration style A, bidimensional array: 0.251000 Total time style A: 1.993000 Fill style B, single-dimension array: 3.414000 Iteration style B, single-dimension array: 0.271000 Total time style B: 3.695000 Fill style A, bidimensional array: 2.073000 Iteration style A, bidimensional array: 0.240000 Total time style A: 2.313000 Fill style B, single-dimension array: 3.846000 Iteration style B, single-dimension array: 0.310000 Total time style B: 4.166000 Fill style A, bidimensional array: 2.133000 Iteration style A, bidimensional array: 0.251000 Total time style A: 2.384000 Fill style B, single-dimension array: 3.735000 Iteration style B, single-dimension array: 0.350000 Total time style B: 4.085000 Fill style A, bidimensional array: 2.144000 Iteration style A, bidimensional array: 0.240000 Total time style A: 2.384000 Fill style B, single-dimension array: 3.795000 Iteration style B, single-dimension array: 0.381000 Total time style B: 4.176000 ----------------------------------------------------------------- import time def TimeMS(): return time.time()*1000 def TestA(): global gTestA gTestA = {} for a in xrange(0,999):
       gTestA[a] = {}
       for b in xrange(0,999):
           gTestA[a][b] = "%s%s" % (a,b)
def TestB(): global gTestB gTestB = {} for a in xrange(0,999):
       for b in xrange(0,999):
           gTestB["%s%s" % (a,b)] = "%s%s" % (a,b)
def TestA2(): global gTestA for a in gTestA:
       for b in gTestA[a]:
           c = gTestA[a][b]
def TestB2(): global gTestB for a in gTestB:
       c = gTestB[a]
# TEST: for n in xrange(1,5): gTestA = {} print "\nFill style A, bidimensional array: ", startt = TimeMS() TestA() print "%f" % ((TimeMS() - startt)/1000) print "Iteration style A, bidimensional array: ", startt2 = TimeMS() TestA2() print "%f" % ((TimeMS() - startt2)/1000) print "Total time style A: %f" % ((TimeMS() - startt)/1000) gTestB = {} print "\nFill style B, single-dimension array: ", startt = TimeMS() TestB() print "%f" % ((TimeMS() - startt)/1000) print "Iteration style B, single-dimension array: ", startt2 = TimeMS() TestB2() print "%f" % ((TimeMS() - startt2)/1000) print "Total time style B: %f" % ((TimeMS() - startt)/1000)

« previous php.general (#236177) next »