Python Fundamentals: Test the type and isinstance that faster

Source: Internet
Author: User

Nonsense not to say, directly on the source

1 #Decorative Device2 Import Time3 defRuntime (func):4     defInner_func (*args, * *Kwargs):5StartTime =time.time ()6res = func (*args)7         Print('Run time:{}'. Format (Time.time ()-startTime))8         returnRes9     returnInner_funcTen  One #testing the type function A defTest_type (*args, * *Kwargs): -types = [Str,int,float,bool,tuple,list,set,dict]#define a list of common types -type_dict= {}#defines a return set type dictionary the     ifArgs#determine if there are parameters -          forArginchArgs#Traversal Parameters -              forTinchTypes#Traversal Type -                 ifType (ARG) = = T:#match types with type function +TYPE_DICT[STR (arg)] = t#convert arg to key name, T as key value into dictionary -                      Break                             #Interrupt Loop +     returntype_dict A  at #testing the Isinstance function - defTest_isinstance (*args, * *Kwargs): -Types =[Str,int,float,bool,tuple,list,set,dict] -type_dict= {} -     ifargs: -          forArginchargs: in              forTinchtypes: -                 ifIsinstance (ARG, t) = = True:#match type with isinstance function toTYPE_DICT[STR (ARG)] =T +                      Break -     returntype_dict the  * @runtime $ defFor_loop_type (*args, * *Kwargs):Panax Notoginseng      forIinchRange (100000):#Loop call Test_type function 100,000 times - Test_type (args, Kwargs) the  + @runtime A defFor_loop_isinstance (*args, * *Kwargs): the      forIinchRange (10000):#Loop call test_isinstance function 100,000 times + test_isinstance (args, Kwargs) -          $          $ #For_loop_type (1,2,3,4,5,6,7,8,9,2,0,true, (' A ', ' B ', ' C '), {' A ', ' B ', ' C '},[' A ', ' B ', ' C '],{' name ': ' Tom ', ' age ': 20 }) - #for_loop_isinstance (1,2,3,4,5,6,7,8,9,2,0,true, (' A ', ' B ', ' C '), {' A ', ' B ', ' C '},[' A ', ' B ', ' C '],{' name ': ' Tom ', ' Age ': $})

Small partners to test it, I measured the data show that isinstance than the type of high efficiency is not a level, after the test you have to know.

Python Fundamentals: Test the type and isinstance that faster

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.