Skip to content

Instantly share code, notes, and snippets.

@mgermain
Last active August 29, 2015 14:17
Show Gist options
  • Save mgermain/cb5b0b8e9d3ed8a61baf to your computer and use it in GitHub Desktop.
Save mgermain/cb5b0b8e9d3ed8a61baf to your computer and use it in GitHub Desktop.
Compare the speed of appending to a file VS reading everything in memory appending there then rewriting everything.
"""
## RESULTS ##
# Windows 8.1 + NTFS + SSD:
read_write 6.81500005722
append 6.42299985886
# LinuxMint 15 + ext4 witout journaling + HD
read_write 0.222398996353
append 0.00950384140015
"""
import os
import time as t
# to_write = ["eop" * 10000 + str(i) for i in range(1000)]
to_write = ["eop" * 10 + str(i) for i in range(1000)]
append_filename = "test_append.txt"
rw_filename = "test_read_write.txt"
def append(wop):
with open(append_filename, 'a') as f:
f.write(wop)
def read_write(wop):
text = ""
if os.path.exists(rw_filename):
with open(rw_filename, 'r') as f:
text = f.read()
with open(rw_filename, 'w') as f:
f.write(text + wop)
def test(func, f):
if os.path.exists(f):
os.remove(f)
startt = t.time()
for wop in to_write:
func(wop)
return t.time() - startt
print "read_write", test(read_write, rw_filename)
print "append", test(append, append_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment