I'm running some simulations that were going too slow, so I profiled my code and found that over 90 percent of the time was being spent converting a (2D) numpy array to a string, as in:
arr = np.ones(25000).reshape(5000,5)
s = '\n'.join('\t'.join([str(x) for x in row]) for row in arr]
I tried a bunch of different solutions (using map, converting the array using astype(str), casting to a list) but most gave only marginal improvement.
Eventually I gave up on trying to convert the array to a string and saved it to a file on its own using np.save(arr), which gave a 2000x(!) speedup. Is there a way to write the array as a text file with similar performance?