Unexpected timing results with file I/O

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Feb 4 18:08:02 CET 2008


On Mon, 04 Feb 2008 15:17:18 +0000, Steven D'Aprano wrote:

> # Method one: grouped by file.
> for each file:
>     open the file, append the string, then close it
> 
> 
> # Method two: grouped by procedure.
> for each file:
>     open the file
> for each open file:
>     append the string
> for each open file:
>     close the file
>
> Method 1:
> 
> 17.391216039657593
> 
> Method 2:
> 
> 16.823362112045288
> 
> 
> Surprisingly, Method 2 is a smidgen faster, by about half a second over 
> 500,000 open-write-close cycles. It's not much faster, but it's 
> consistent, over many tests, changing many of the parameters (e.g. the 
> number of files, the number of runs per timeit test, etc.).
> 
> I'm using Linux and Python 2.5.
> 
> So, what's going on? Can anyone explain why the code which does more work 
> takes less time?

Can't confirm this (Linux, Python 2.5):

Method 1: 15.380897998809814
Method 2: 18.085366010665894

I guess it's really all about the disk IO as my system monitor applet
shows that almost all of the time is spend in the kernel and very little
in user space.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list