Skip to content

Instantly share code, notes, and snippets.

@Weiyuan-Lane
Last active February 15, 2021 18:44
Show Gist options
  • Save Weiyuan-Lane/7e43cdfa55e37a5d86bb67251dc66327 to your computer and use it in GitHub Desktop.
Save Weiyuan-Lane/7e43cdfa55e37a5d86bb67251dc66327 to your computer and use it in GitHub Desktop.
Comparison of file writing strategies
user system total real
Write all lines to file at once, mode "w" 0.270430 0.901323 1.171753 ( 1.193794)
Write all lines to file at once, mode "a" 0.214400 0.794183 1.008583 ( 1.023748)
Write all lines to file at once, mode "w+" 0.222848 0.774039 0.996887 ( 1.004640)
Write all lines to file at once, mode "a+" 0.270665 0.777068 1.047733 ( 1.074217)
Iterate and write per line to file, mode "w" 0.017369 0.021650 0.039019 ( 0.060581)
Iterate and write per line to file, mode "a" 0.016263 0.000707 0.016970 ( 0.036163)
Iterate and write per line to file, mode "w+" 0.015498 0.001238 0.016736 ( 0.037824)
Iterate and write per line to file, mode "a+" 0.014625 0.001813 0.016438 ( 0.036621)
Iterate and write per line to file (manual), mode "w" 0.017979 0.000257 0.018236 ( 0.042628)
Iterate and write per line to file (manual), mode "a" 0.018012 0.000126 0.018138 ( 0.039839)
Iterate and write per line to file (manual), mode "w+" 0.018808 0.001389 0.020197 ( 0.046190)
Iterate and write per line to file (manual), mode "a+" 0.020009 0.000105 0.020114 ( 0.050143)
Iterate and write per line to file chunks, mode "w" 0.032448 0.005141 0.037589 ( 0.177057)
Iterate and write per line to file chunks, mode "a" 0.044986 0.007247 0.052233 ( 0.194858)
Iterate and write per line to file chunks, mode "w+" 0.031080 0.006523 0.037603 ( 0.113953)
Iterate and write per line to file chunks, mode "a+" 0.022506 0.002483 0.024989 ( 0.102055)
Iterate and write per line to file chunks (hardcode), mode "w" 0.022638 0.001287 0.023925 ( 0.144586)
Iterate and write per line to file chunks (hardcode), mode "a" 0.020493 0.002158 0.022651 ( 0.119901)
Iterate and write per line to file chunks (hardcode), mode "w+" 0.019646 0.001023 0.020669 ( 0.094777)
Iterate and write per line to file chunks (hardcode), mode "a+" 0.018822 0.001380 0.020202 ( 0.093362)
Iterate and write per line to file chunks (manual), mode "w" 0.022695 0.000775 0.023470 ( 0.096073)
Iterate and write per line to file chunks (manual), mode "a" 0.025606 0.000505 0.026111 ( 0.168614)
Iterate and write per line to file chunks (manual), mode "w+" 0.026715 0.002807 0.029522 ( 0.160476)
Iterate and write per line to file chunks (manual), mode "a+" 0.022942 0.003000 0.025942 ( 0.115456)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment