Created
March 30, 2022 18:11
-
-
Save nschloe/c600257ce94e637bec7fdaedf1611697 to your computer and use it in GitHub Desktop.
Python/NumPy integer ASCII writes
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 perfplot | |
import numpy as np | |
def setup(n): | |
return np.random.randint(0, 100, (n, 4)) | |
def loop(data): | |
with open("f.txt", "w") as f: | |
for gid, row in enumerate(data): | |
f.write(f"{gid} " + " ".join(str(val) for val in row) + "\n") | |
def savetxt(data): | |
gids = np.arange(len(data)) | |
with open("g.txt", "w") as f: | |
np.savetxt(f, np.column_stack([gids, data]), fmt='%d') | |
def tofile(data): | |
gids = np.arange(len(data)) | |
with open("h.txt", "w") as f: | |
np.column_stack([gids, data]).tofile(f, sep=" ") | |
b = perfplot.bench( | |
setup=setup, | |
kernels=[loop, savetxt, tofile], | |
n_range=[2**k for k in range(21)], | |
equality_check=None | |
) | |
b.show() |
Author
nschloe
commented
Mar 30, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment