NumPy get started with an example vectorsum.py

Source: Internet
Author: User

The NumPy system is an open-source numerical extension of Python. This tool can be used to store and manipulate large matrices, which is much more efficient than Python's own nested list (nested list structure) structure, which is also useful for representing matrices (matrix). It is said that NumPy Python is the equivalent of becoming a free, more powerful MATLAB system. The numpy itself is implemented in C.

Turning over the study guide of Packt Publishing House, the first chapter has a comparison of computational efficiency examples to get started.

As a result, in the native 32bit Python run out of the effect of a lot of differences with the book, the first is the 32bit python shaping limit and 64bit a lot of difference:

>>> sys.maxsize2147483647

There is also a problem with datetime timing intensive reading.

Anyway, just try it,

The code is below and the problem is solved StackOverflow

#-*-coding:utf-8-*-ImportSYS fromDatetimeImportdatetimeImportNumPy as NP"""Input format:python vectorsum.py n n:the excepted size for the vector"""defnumpsysum (N): a= Np.arange (N) * * 2b= Np.arange (N) * * 3C= A +breturnCdefpythonsum (N): a=range (n) b=range (n) C= []     forIinchRange (len (a)): A[i]= i * * 2B[i]= i * * 3c.append (A[i]+B[i])returnC size= Int (sys.argv[1]) Start=DateTime.Now () C=pythonsum (size) Delta= DateTime.Now ()-StartPrint "The last 2 elements of the pythonsum are:", c[-2:]Print "Python method Cost Time:", Delta.microsecondsstart=DateTime.Now () C=numpsysum (size) Delta= DateTime.Now ()-StartPrint "The last 2 elements of the numpysum are:", c[-2:]Print "NumPy method Cost Time:", Delta.microseconds

There is no demand for mass processing at the moment. Souvenir ~

Note: The python maxsize that comes with Linux is 9223372036854775807

NumPy Get started with an example vectorsum.py

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.