Last active
April 3, 2020 08:21
-
-
Save rjodon/58b8e09b05f8ae7637d3eb8636e1ef87 to your computer and use it in GitHub Desktop.
bench_numpy_savetxt_vs_io_write.ipynb
This file contains hidden or 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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "bench_numpy_savetxt_vs_io_write.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyMbSHqmJk2HKGeF/r9uOIVm", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/rjodon/58b8e09b05f8ae7637d3eb8636e1ef87/untitled0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "SWprhi_j6_IE", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"\"\"\"\n", | |
"Small benchmark of numpy.savetxt() vs python io.open + io.write\n", | |
"\n", | |
"* I am using time plus an explicit loop to profile the snippet\n", | |
"* Why? timeit can not control python caches here. Thus, results would not reflect reality. \n", | |
"\n", | |
"\"\"\"\n", | |
"import numpy\n", | |
"\n", | |
"nc = 1; nb = 20; ni = 6; nc = 2; ia = 20; ib = 20; ic = 0\n", | |
"scale_factor = 10000\n", | |
"\n", | |
"U1 = numpy.array(range(0, scale_factor))\n", | |
"U2 = numpy.array(range(scale_factor, 2*scale_factor))\n", | |
"U3 = numpy.array(range(3*scale_factor, 4*scale_factor))\n", | |
"U4 = numpy.array(range(4*scale_factor, 5*scale_factor))\n", | |
"U5 = numpy.array(range(5*scale_factor, 6*scale_factor))\n", | |
"\n", | |
"a = nc*(nb*nc*ia+nc*ib+ic)+U1\n", | |
"a2 = ia + U1\n", | |
"b2 = ib + U3\n", | |
"c2 = ic + U4\n", | |
"b = nc * (nb * nc * a2 + nc * b2 + c2) + U2\n", | |
"A = numpy.array((a, b, U5)).T\n" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "kxGXssI-7Bca", | |
"colab_type": "code", | |
"outputId": "1f979bb3-d217-4d95-ebda-2a2fff9b6fa3", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 49 | |
} | |
}, | |
"source": [ | |
"%%time\n", | |
"for i in range(100):\n", | |
" numpy.savetxt(\"savetxt.txt\", A)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 2.73 s, sys: 108 ms, total: 2.84 s\n", | |
"Wall time: 2.91 s\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "3HMFGSf77DAH", | |
"colab_type": "code", | |
"outputId": "3b6ead01-578a-4b8f-84b1-84a99ce543ef", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 49 | |
} | |
}, | |
"source": [ | |
"%%time\n", | |
"for i in range(100):\n", | |
" with open(file=\"write.txt\", mode=\"w\") as b:\n", | |
" b.write(numpy.array2string(A))" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 12.9 ms, sys: 6 ms, total: 18.9 ms\n", | |
"Wall time: 121 ms\n" | |
], | |
"name": "stdout" | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Linked to: https://stackoverflow.com/questions/60991392/writing-a-numpy-array-in-a-textfile/60991651#60991651