Advertisement
Guest User

Actually Understanding Programming Helps

a guest
Mar 27th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import timeit
  2. from array import array
  3. numAdd = 1 #Additional changes are needed when numAdd = 1000000
  4. totalMemory = 1000000 # bytes
  5.  
  6. #global concatString # global ensures a slow-down under Linux
  7. addString = ""
  8. for i in range(0, numAdd):
  9.  addString = addString + "1"
  10.  
  11. char_array = array('c')
  12.  
  13. # First part: in-memory
  14. numIter = int(totalMemory / len(addString))
  15. f = open('test.txt','w')
  16. start = timeit.default_timer()
  17. for i in range(0, numIter):
  18. # concatString = addString + concatString # modified: concatString = concatString + addString
  19.   char_array.fromstring("1");
  20. stop = timeit.default_timer()
  21. stringTime = stop - start
  22. start = timeit.default_timer()
  23. f.write(char_array.tostring())
  24. f.flush()
  25. f.close()
  26. stop = timeit.default_timer()
  27. fileTime = stop - start
  28. print "in-memory: String took " + str(stringTime) + ", file took " + str(fileTime)
  29.  
  30. # second part: disk-only
  31. numIter = int(totalMemory / len(addString))
  32. f = open('test.txt','w')
  33. start = timeit.default_timer()
  34. for i in range(0, numIter):
  35.  f.write(addString)
  36. f.flush()
  37. f.close()
  38. stop = timeit.default_timer()
  39. fileTime = stop - start
  40. print "disk-only: file took " + str(fileTime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement