[Tutor] Uniques values in a list - testing functions

Alexandre Ratti alex@gabuzomeu.net
Mon, 18 Mar 2002 15:16:19 +0100


Hello,


At 15:50 17/03/2002 -0500, Raymond Hettinger wrote:
>While we're building a test bench, I have a couple of
>additions.  Do a full garbage collection just before the
>timing starts -- this will reduce distortions due to gc.
>Also, I would use xrange so that we don't eat-up
>a meg of memory (100000 iters * (4 bytes per int + 4 bytes
>per pointer to the int) ) before the function starts running.
>At the end, print an excerpt of the results and its length.
>That way you'll have some small assurance that the
>function is really doing what it's supposed to.

Yesterday I wrote a short test class. The results are copied below. In 
"Timer.testLists(n, m)", n is the data set size (number of items in the 
list passed to the functions) and m is the number of runs (number of times 
the functions are run).

The simple dictionary-based solution looks consistently faster, though the 
list comprehension version is close for large datasets.

If anyone is interested, the test code is here (you need both Timer and 
ListUtils modules):

         http://alexandre.ratti.free.fr/python/

If you find out that these tests are flawed, please let me know.


Cheers.

Alexandre


 >>> Timer.testLists(500, 1000)

Function name   Total runtime   Runtime per run
filterListWithDict      0.386876        0.000387
filterListWithMap22     0.421740        0.000422
filterListWithMap21     0.429549        0.000430
filterListWithComprehension22   0.547386        0.000547
filterListWithMapAndOperator    0.587310        0.000587
filterListWithComprehension21   0.963933        0.000964
filterListInPlace       2.015952        0.002016
filterListWithList      2.201321        0.002201

 >>> Timer.testLists(5000, 100)

filterListWithDict      0.358967        0.003590
filterListWithComprehension22   0.383897        0.003839
filterListWithMap22     0.392658        0.003927
filterListWithMap21     0.393884        0.003939
filterListWithMapAndOperator    0.529176        0.005292
filterListWithComprehension21   0.800180        0.008002
filterListInPlace       1.994437        0.019944
filterListWithList      2.189302        0.021893

 >>> Timer.testLists(50000, 30)

filterListWithDict      1.084748        0.036158
filterListWithComprehension22   1.141160        0.038039
filterListWithMap22     1.211721        0.040391
filterListWithMap21     1.212633        0.040421
filterListWithMapAndOperator    1.655985        0.055199
filterListWithComprehension21   2.386100        0.079537
filterListInPlace       6.323484        0.210783
filterListWithList      6.565961        0.218865

 >>> Timer.testLists(100000, 20)

filterListWithDict      1.430084        0.071504
filterListWithComprehension22   1.502246        0.075112
filterListWithMap21     1.637107        0.081855
filterListWithMap22     1.673499        0.083675
filterListWithMapAndOperator    2.202443        0.110122
filterListWithComprehension21   3.130225        0.156511
filterListInPlace       8.386927        0.419346
filterListWithList      8.685972        0.434299