I donβt know if you continue to follow this issue, but I found your question interesting, so I tried it on a Linux laptop.
I ran your code in python 3.5 and found that you need to have the os.O_SYNC flag to avoid a buffering problem (basically, the os.write function os.write not return before all the data is written to disk). I also replace time.clock() with time.time() , which give better results.
import os import time import cProfile def ioTest(): block = bytes('A'*10*1024*1024, 'utf-8') filename = 'test.bin' f = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_SYNC) start = time.time() for x in range(500): os.write(f,block) os.close(f) transferTime_sec = time.time() - start msg = 'Wrote {:0f}MB in {:0.03f}s' print(msg.format(os.stat(filename).st_size/1024/1024, transferTime_sec)) cProfile.run('ioTest()')
In addition, this post talks about using the os.O_DIRECT flag, which will use DMA and avoid bottlenecks. I had to use the mmap module to make it work on my machine:
import os import time import cProfile import mmap def ioTest(): m = mmap.mmap(-1, 10*1024*1024) block = bytes('A'*10*1024*1024, 'utf-8') m.write(block) filename = 'test.bin' f = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_SYNC, os.O_DIRECT) start = time.time() for x in range(500): os.write(f,m) os.close(f) transferTime_sec = time.time() - start msg = 'Wrote {:0f}MB in {:0.03f}s.' print(msg.format(os.stat(filename).st_size/1024/1024, transferTime_sec)) cProfile.run('ioTest()')
This reduced the recording time on my machine by 40% ... not bad. I did not use os.O_SEQUENTIAL and os.O_BINARY which are not available on my machine.
[Edit] : I found how to use the os.O_DIRECT flag from this site , which explains it very well and in detail. I highly recommend reading this if you're interested in performance and direct I / O in Python.
Tom r source share