How Faster Numpy Array Compare To Python List

Numpy array operation is faster than python list, it also cost less memory than python list. With a Numpy array, you do not need to create a loop to operate all array elements, you can operate all array elements just use one line code.

This example will create a Numpy array and a python list that has 1000000 elements, and then calculate each element’s quadratic value and print out both Numpy and python list execution times. You can see which one is faster.

1. Compare Numpy And Python List Operation Example.

  1. First, you should startup jupter notebook web server in a terminal with the below command.
    ~$ jupyter notebook
  2. Then login to the jupyter notebook website, and create a new jupyter notebook file ( .ipynb).
  3. Input below ipython source code in one line cell and run it. Please note the ipython %time command, it can print out the times that the code execution cost.
    import numpy as np
    # create a Numpy array.
    my_arr = np.arange(1000000)
    # create a python list.
    my_list = list(range(1000000))
    # print out execution time for 
    %time for _ in range(10): my_arr2 = my_arr ** 2
    %time for _ in range(10): my_list2 = [x ** 2 for x in my_list]
  4. When you run above line cell code, you will get below output. We can see that the Numpy array runs very fast than the python list.
    CPU times: user 18.3 ms, sys: 0 ns, total: 18.3 ms
    Wall time: 19.7 ms
    CPU times: user 2.12 s, sys: 107 ms, total: 2.23 s
    Wall time: 2.24 s
  5. If you print out the Numpy array and python list values in iPython, you can get the below result, Numpy array data can be printed out, but python list can not because of python list use more memory so the jupyter notebook server will crash if it prints out python list elements.
    [     0      1      2 ... 999997 999998 999999]
    [           0            1            4 ... 999994000009 999996000004 999998000001]
    IOPub data rate exceeded.
    The notebook server will temporarily stop sending output
    to the client in order to avoid crashing it.
    To change this limit, set the config variable
    Current values:
    NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)
    NotebookApp.rate_limit_window=3.0 (secs)

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.