Data structure and algorithm (II.)

Source: Internet
Author: User

Python built-in type performance analysis

Timeit Module

Class Timeit. Timer (stmt= "Pass", setup= ' Pass ', Time=<timer function>)

A timer is a class that measures the execution speed of a small piece of code

The stmt parameter is the code statement to be tested (statment)

The setup parameter is the setting that is required to run the code

The timer parameter is a timer function, independent of the platform.

Timeit. Timer.timeit (number=1000000)

The object method that tests the execution speed of the statement in the Timer class, the number parameter is the test book count when the code is measured, and the default is 1 million times. method returns the average time spent executing code, and the number of seconds for a float type.

def t1 ():
Li = []
For I in range (10000):
Li.append (i)

Def t2 ():
Li = []
For I in range (10000):
Li + = [i]

def t3 ():
Li = [i for I in range (10000)]

def t4 ():
Li = List (range (10000))

Def T5 ():
Li = []
For I in range (10000):
Li.extend ([i])


Timer1 = Timeit ("T1 ()", "from __main__ import T1", number=1000)
Print ("Append:", timer1)

Timer2 = Timeit ("T2 ()", "from __main__ import T2", number=1000)
Print ("+:", Timer2)


Timer3 = Timeit ("t3 ()", "from __main__ import T3", number=1000)
Print ("I for I in range (n):", Timer3)

Timer4 = Timeit ("t4 ()", "from __main__ import T4", number=1000)
Print ("List (range (n)):", Timer4)

Timer5 = Timeit ("T5 ()", "from __main__ import T5", number=1000)
Print ("Li.extend ([i]):", Timer5)

Data structure and algorithm (II.)

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.