Skip to content

Instantly share code, notes, and snippets.

@nschloe
Created March 30, 2022 18:11
Show Gist options
  • Save nschloe/c600257ce94e637bec7fdaedf1611697 to your computer and use it in GitHub Desktop.
Save nschloe/c600257ce94e637bec7fdaedf1611697 to your computer and use it in GitHub Desktop.
Python/NumPy integer ASCII writes
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()
@nschloe
Copy link
Author

nschloe commented Mar 30, 2022

Figure_1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment