How fast is Python's built-in sort

Source: Internet
Author: User

I compared the run time of sort and a program I wrote O (n).
Marvel finds the speed of sort almost as close as the program run Time of O (n) written directly in Python
first on the code
test code for sort
Import Random
Import SYS
len_test_arr = Int (sys.argv[1])
Test_arr = [Random.random () for I in Xrange (Len_test_arr)]
Len_test_arr.sort ()
The run instruction for the test time Python test.py 10,10 represents the length of the array, which is used for the duration of the user.


python-written o (n) program
Import Random
Import SYS


def Counting_sort (a):
count = [0]*100
For I in a:
count + = 1
return Count


len_test_arr = Int (sys.argv[1])
Test_arr = [Random.randint (0) for I in Xrange (Len_test_arr)]
Counting_sort (Test_arr)
The run instructions for the test time are the same. The test.py 10,10 represents the length of the array, and only the user is taken.


Results
built-in sort
Data Size | Run time (s)
Ten 0.01
0.014
0.01
10000 0.013
100000 0.065
1000000 0.769
10000000 10.991


python-written o (n) program
Data Size | Run time (s)
Ten 0.01
0.01
0.01
10000 0.02
100000 0.13
1000000 1.1
10000000

How fast is Python's built-in sort

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.