Python in vs ==. What to use in this case?

I make an AJAX call in it and pass the pub variable, which can be 1 or 0 .

As a beginner, I want to be sure that the type of the variable will be twice as high. I know that I can easily convert to int() , and the problem is not really related to the AJAX result, but this led to this question.

My code is:

 if pub == 1 or pub == '1': #execute funcA() 

But for me this is not the case for me, so I tried:

 if pub in [1,'1']: #execute funcA() 

Which of the above codes is better in terms of:

  • Performance (speed).
  • Best practice.
  • Memory usage.
+7
python
source share
5 answers

Performance : Better

 timeit.timeit("pub='1'; pub == 1 or pub == '1'") 0.07568907737731934 timeit.timeit("pub='1'; pub in[1, '1']") 0.04272890090942383 timeit.timeit("pub=1; pub == 1 or pub == '1'") 0.07502007484436035 timeit.timeit("pub=1; pub in[1, '1']") 0.07035684585571289 #other options timeit.timeit("pub='1'; pub in (1,'1')") 0.04643988609313965 timeit.timeit("pub='1'; pub in {1,'1'}") 0.17076611518859863 timeit.timeit("pub=1; pub in (1,'1')") 0.047419071197509766 timeit.timeit("pub=1; pub in {1,'1'}") 0.1770930290222168 

So {} > or > [] > () depending on performance.

Practice : in is preferred because its type is smaller. (), [], {} equally well based on practice

Memory

 sys.getsizeof([1,"1"]) 88 sys.getsizeof("1",1) 38 #other options sys.getsizeof(("1",1)) 72 sys.getsizeof({"1",1}) 232 

So {} > [] > () > or based on memory

Although not asked, it's nice to know:

Functionality : equality of value, not reference equality

in is just a sequential equality test == . So similar. in uses == , not is . I want to say the following:

 >>> a = [1,2,3] >>> b = [1,a] >>> b [1, [1, 2, 3]] >>> 1 in b True >>> a in b True >>> [1,2,3] in b True 

So, it is not implemented as follows:

 >>> for i in b: ... print [1,2,3] is i ... False False 

is will return True if two variables point to the same object, == if the objects referenced by the variables are equal. in uses ==

+12
source share

This code is better

 if pub in [1,'1']: #execute funcA() 

because it is a little faster, but mainly because it is not redundant . The pub variable appears only once there.

+7
source share

Thus, in skill is better:

 timeit.timeit("pub='1'; pub == 1 or pub == '1'") 0.16224503758795805 timeit.timeit("pub='1'; pub in[1, '1']") 0.13723585976354258 timeit.timeit("pub=1; pub == 1 or pub == '1'") 0.07986264585216674 timeit.timeit("pub=1; pub in[1, '1']") 0.07246544186018866 

And according to the memory size, if better, since the list uses a little more memory

 sys.getsizeof([1,"1"]) 44 sys.getsizeof(1) 12 sys.getsizeof("1") 22 sys.getsizeof("1",1) 22 

This is because even creating an empty list requires memory space.

 sys.getsizeof([]) 36 

I do not know about best practice aspect

For me, the most pythonic way would be to use in , as it reduces input

+3
source share

1. Performance:

 >>> timeit.timeit("pub=1; pub == 1 or pub == '1'", number=10000) 0.0017161369323730469 >>> timeit.timeit("pub=1; pub in[1, '1']", number=10000) 0.0013611316680908203 

2. Best practice: It’s good to write a Python program, I prefer (pub in [1, '1])

3. Memory usage: number 1 and line '1' are always cached in python, you can check refcount for these objects. Therefore, ideally, this will not take any additional memory.

 >>> sys.getrefcount(1) 833 >>> sys.getrefcount('1') 16 

If you use a list that needs more memory, you must allocate those cached objects to send. (below in 64-bit type)

 >>> sys.getsizeof([1, '1']) 88 

These 88 bytes of memory you will allocate more than another way.

I suggest going with:

 if pub in [1,'1']: #execute funcA() 
+1
source share

In speed:
in faster and more.
To prove this, here is the code.

 from datetime import datetime start0 = datetime.now() pub = 1 if pub == 1 or pub == '1': # execute funcA() pass end0 = datetime.now() - start0 print end0 start1 = datetime.now() if pub in [1, '1']: # execute funcA() pass end1 = datetime.now() - start1 print end1 print end0 - end1 

Output:

 0:00:00.000045 0:00:00.000007 0:00:00.000038 

In space: == much better.
in takes O (2) space, and == takes O (1) space

In my opinion, the best practice is to use in , as it is much faster and saves a lot of input

0
source share

All Articles