<i id='3d98M'><tr id='3d98M'><dt id='3d98M'><q id='3d98M'><span id='3d98M'><b id='3d98M'><form id='3d98M'><ins id='3d98M'></ins><ul id='3d98M'></ul><sub id='3d98M'></sub></form><legend id='3d98M'></legend><bdo id='3d98M'><pre id='3d98M'><center id='3d98M'></center></pre></bdo></b><th id='3d98M'></th></span></q></dt></tr></i><div id='3d98M'><tfoot id='3d98M'></tfoot><dl id='3d98M'><fieldset id='3d98M'></fieldset></dl></div>
  1. <tfoot id='3d98M'></tfoot>
  2. <legend id='3d98M'><style id='3d98M'><dir id='3d98M'><q id='3d98M'></q></dir></style></legend>

      <bdo id='3d98M'></bdo><ul id='3d98M'></ul>

    1. <small id='3d98M'></small><noframes id='3d98M'>

      为什么泡菜比 np.save 花费这么多时间?

      Why does pickle take so much longer than np.save?(为什么泡菜比 np.save 花费这么多时间?)

      <small id='boNaZ'></small><noframes id='boNaZ'>

          <bdo id='boNaZ'></bdo><ul id='boNaZ'></ul>

          <tfoot id='boNaZ'></tfoot>
          <i id='boNaZ'><tr id='boNaZ'><dt id='boNaZ'><q id='boNaZ'><span id='boNaZ'><b id='boNaZ'><form id='boNaZ'><ins id='boNaZ'></ins><ul id='boNaZ'></ul><sub id='boNaZ'></sub></form><legend id='boNaZ'></legend><bdo id='boNaZ'><pre id='boNaZ'><center id='boNaZ'></center></pre></bdo></b><th id='boNaZ'></th></span></q></dt></tr></i><div id='boNaZ'><tfoot id='boNaZ'></tfoot><dl id='boNaZ'><fieldset id='boNaZ'></fieldset></dl></div>
              <tbody id='boNaZ'></tbody>
          1. <legend id='boNaZ'><style id='boNaZ'><dir id='boNaZ'><q id='boNaZ'></q></dir></style></legend>

              • 本文介绍了为什么泡菜比 np.save 花费这么多时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想保存 dict 或数组.

                我尝试使用 np.savepickle 并发现前者总是花费更少的时间.

                I try both with np.save and with pickle and see that the former always take much less time.

                我的实际数据要大得多,但我只是在这里展示一小块用于演示目的:

                My actual data is much bigger but I just present a small piece here for demonstration purposes:

                import numpy as np
                #import numpy.array as array
                import time
                import pickle
                
                b = {0: [np.array([0, 0, 0, 0])], 1: [np.array([1, 0, 0, 0]), np.array([0, 1, 0, 0]), np.array([0, 0, 1, 0]), np.array([0, 0, 0, 1]), np.array([-1,  0,  0,  0]), np.array([ 0, -1,  0,  0]), np.array([ 0,  0, -1,  0]), np.array([ 0,  0,  0, -1])], 2: [np.array([2, 0, 0, 0]), np.array([1, 1, 0, 0]), np.array([1, 0, 1, 0]), np.array([1, 0, 0, 1]), np.array([ 1, -1,  0,  0]), np.array([ 1,  0, -1,  0]), np.array([ 1,  0,  0, -1])], 3: [np.array([1, 0, 0, 0]), np.array([0, 1, 0, 0]), np.array([0, 0, 1, 0]), np.array([0, 0, 0, 1]), np.array([-1,  0,  0,  0]), np.array([ 0, -1,  0,  0]), np.array([ 0,  0, -1,  0]), np.array([ 0,  0,  0, -1])], 4: [np.array([2, 0, 0, 0]), np.array([1, 1, 0, 0]), np.array([1, 0, 1, 0]), np.array([1, 0, 0, 1]), np.array([ 1, -1,  0,  0]), np.array([ 1,  0, -1,  0]), np.array([ 1,  0,  0, -1])], 5: [np.array([0, 0, 0, 0])], 6: [np.array([1, 0, 0, 0]), np.array([0, 1, 0, 0]), np.array([0, 0, 1, 0]), np.array([0, 0, 0, 1]), np.array([-1,  0,  0,  0]), np.array([ 0, -1,  0,  0]), np.array([ 0,  0, -1,  0]), np.array([ 0,  0,  0, -1])], 2: [np.array([2, 0, 0, 0]), np.array([1, 1, 0, 0]), np.array([1, 0, 1, 0]), np.array([1, 0, 0, 1]), np.array([ 1, -1,  0,  0]), np.array([ 1,  0, -1,  0]), np.array([ 1,  0,  0, -1])], 7: [np.array([1, 0, 0, 0]), np.array([0, 1, 0, 0]), np.array([0, 0, 1, 0]), np.array([0, 0, 0, 1]), np.array([-1,  0,  0,  0]), np.array([ 0, -1,  0,  0]), np.array([ 0,  0, -1,  0]), np.array([ 0,  0,  0, -1])], 8: [np.array([2, 0, 0, 0]), np.array([1, 1, 0, 0]), np.array([1, 0, 1, 0]), np.array([1, 0, 0, 1]), np.array([ 1, -1,  0,  0]), np.array([ 1,  0, -1,  0]), np.array([ 1,  0,  0, -1])]}
                
                
                start_time = time.time()
                with open('testpickle', 'wb') as myfile:
                    pickle.dump(b, myfile)
                print("--- Time to save with pickle: %s milliseconds ---" % (1000*time.time() - 1000*start_time))
                
                start_time = time.time()
                np.save('numpy', b)
                print("--- Time to save with numpy: %s milliseconds ---" % (1000*time.time() - 1000*start_time))
                
                start_time = time.time()
                with open('testpickle', 'rb') as myfile:
                    g1 = pickle.load(myfile)
                print("--- Time to load with pickle: %s milliseconds ---" % (1000*time.time() - 1000*start_time))
                
                start_time = time.time()
                g2 = np.load('numpy.npy')
                print("--- Time to load with numpy: %s milliseconds ---" % (1000*time.time() - 1000*start_time))
                

                给出输出:

                --- Time to save with pickle: 4.0 milliseconds ---
                --- Time to save with numpy: 1.0 milliseconds ---
                --- Time to load with pickle: 2.0 milliseconds ---
                --- Time to load with numpy: 1.0 milliseconds ---
                

                根据我的实际大小(字典中约 100,000 个键),时差更加明显.

                The time difference is even more pronounced with my actual size (~100,000 keys in the dict).

                为什么 pickle 的保存和加载时间都比 np.save 长?

                Why does pickle take longer than np.save, both for saving and for loading?

                我应该什么时候使用 pickle?

                When should I use pickle?

                推荐答案

                因为只要写入的对象不包含Python数据,

                Because as long as the written object contains no Python data,

                • numpy 对象在内存中的表示方式比 Python 对象简单得多
                • numpy.save 是用 C 编写的
                • numpy.save 以需要最少处理的超简单格式写入

                同时

                • Python 对象有很多开销
                • pickle 是用 Python 编写的
                • pickle 将数据从内存中的底层表示转换为写入磁盘上的字节

                请注意,如果一个 numpy 数组确实包含 Python 对象,那么 numpy 只会腌制该数组,所有的胜利都会消失.

                Note that if a numpy array does contain Python objects, then numpy just pickles the array, and all the win goes out the window.

                这篇关于为什么泡菜比 np.save 花费这么多时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                  <tbody id='zPcgj'></tbody>
                • <bdo id='zPcgj'></bdo><ul id='zPcgj'></ul>
                  <tfoot id='zPcgj'></tfoot>

                    <small id='zPcgj'></small><noframes id='zPcgj'>

                    <i id='zPcgj'><tr id='zPcgj'><dt id='zPcgj'><q id='zPcgj'><span id='zPcgj'><b id='zPcgj'><form id='zPcgj'><ins id='zPcgj'></ins><ul id='zPcgj'></ul><sub id='zPcgj'></sub></form><legend id='zPcgj'></legend><bdo id='zPcgj'><pre id='zPcgj'><center id='zPcgj'></center></pre></bdo></b><th id='zPcgj'></th></span></q></dt></tr></i><div id='zPcgj'><tfoot id='zPcgj'></tfoot><dl id='zPcgj'><fieldset id='zPcgj'></fieldset></dl></div>

                      1. <legend id='zPcgj'><style id='zPcgj'><dir id='zPcgj'><q id='zPcgj'></q></dir></style></legend>