Last active
February 22, 2019 08:35
-
-
Save xnuinside/2330a878ec3b9ed4497fd2f279f44d69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
i = [str(x) for x in range(1000000)] | |
def funct_write_with_print(): | |
with open('test1.txt', 'w') as fout: | |
print(i, file=fout) | |
def write_with_write(): | |
with open('test12.txt', 'w') as fout: | |
fout.writelines(i) | |
print(timeit.timeit(funct_write_with_print, number=1)) | |
print(timeit.timeit(write_with_write, number=1)) | |
result: | |
print - 0.13328031099990767 | |
writelines - 0.31055811799978983 | |
import timeit | |
i = str([x for x in range(1000000)]) | |
def funct_write_with_print(): | |
with open('test1.txt', 'w') as fout: | |
print(i, file=fout) | |
def write_with_write(): | |
with open('test12.txt', 'w') as fout: | |
fout.writelines(i) | |
print(timeit.timeit(funct_write_with_print, number=1)) | |
print(timeit.timeit(write_with_write, number=1)) | |
result: | |
print - 0.0148353650001809 | |
writelines - 1.7237431819994526 | |
import timeit | |
i = str([x for x in range(1000000)]) | |
def funct_write_with_print(): | |
with open('test1.txt', 'w+') as fout: | |
print(i, file=fout) | |
def write_with_write(): | |
with open('test12.txt', 'w+') as fout: | |
fout.write(i) | |
print(timeit.timeit(write_with_write, number=10)) | |
print(timeit.timeit(funct_write_with_print, number=10)) | |
results: | |
write - 0.20665896299942688 | |
print - 0.20885446599913848 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment