Why is numpy array so fast?

Source: Internet
Author: User
In pythonnumpy, If I generate numpyarray using a list randomly generated with a length of 10 ^ 6, it takes 0.1 s to generate it. However, to obtain the mean of this array, only 2% of the time of init is required. However, it takes more than 10 seconds for the array of implement to get the mean value. So the array of numpy is very black. The technology should be: 1) is the underlying code too powerful? 2) Does partiallycompute have some intermediate values during init? (The mean time should be used in python numpy. If I generate a numpy array Using a list randomly generated with a length of 10 ^ 6, the generation takes 0.1 s, however, the mean value of this array only takes 2% of the init time. However, it takes more than 10 seconds for the array of implement to get the mean value.
Therefore, the array of numpy is very black and the technology should be:
1) is the underlying code too powerful?
2) does partially compute have some intermediate values during init? (Mean time should be slower than access time, faster than O (n)
If it is 2, can you give me a rough idea (mean can be obtained without using python O (n )?
Thank you! Reply: Many numpy functions are implemented not only in C, but also in BLAS (link to MKL in Windows and link to OpenBLAS in Linux ). Basically, those BLAS implementations are highly optimized in each operation, such as using AVX vector instruction sets, which is much faster than your own C implementation, let alone the Python implementation .. You can use blas to test numpy bottom layer using BLAS for vector and matrix operations. Vector operation like averaging can be easily accelerated using multi-threading or vectorization. For example, MKL has many optimizations.

A = []; s = 0; n = 1000000 from time import * from math import * from random import * st = clock () for I in range (n):. append (random () for I in a: s = s + iet = clock () print "mean =", s/n, "time =", et-st, "seconds"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.