[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: which one is more efficient

Christian Heimes

2/10/2008 6:05:00 AM

ki lo wrote:
> I have type variable which may have been set to 'D' or 'E'
>
> Now, which one of following statements are more efficient
>
> if type =='D' or type == 'E':
>
> or
>
> if re.search("D|E", type):
>
> Please let me know because the function is going to called 10s of millions
> of times.

For maximum efficiency you have to use a set. You can speed up the check
even more if you rebind the set in the function declaration. It saves
the global lookup or the creation of the frozen set in every function call.

type_set=frozenset(('D', 'E'))

def yourfunction(self, ..., type_set=type_set):
...
if type in type_set:
...
...

Christian

6 Answers

Paul Rubin

2/10/2008 6:13:00 AM

0

> ki lo wrote:
> > I have type variable which may have been set to 'D' or 'E'
> >
> > Now, which one of following statements are more efficient
> >
> > if type =='D' or type == 'E':
> >
> > or
> >
> > if re.search("D|E", type):
> >
> > Please let me know because the function is going to called 10s of millions
> > of times.
>
> For maximum efficiency you have to use a set.

if type in 'DE': ...

might be faster. Really the only way to know is make actual
measurements.

Bruno Desthuilliers

2/11/2008 12:19:00 PM

0

Paul Rubin a écrit :
>> ki lo wrote:
>>> I have type variable which may have been set to 'D' or 'E'
>>>
>>> Now, which one of following statements are more efficient
>>>
>>> if type =='D' or type == 'E':
>>>
>>> or
>>>
>>> if re.search("D|E", type):
>>>
>>> Please let me know because the function is going to called 10s of millions
>>> of times.
>> For maximum efficiency you have to use a set.
>
> if type in 'DE': ...
>
> might be faster.

It might also be wrong if there's a possibility that type=='DE' :
>>> "DE" in "DE"
True


Steve Holden

2/11/2008 3:42:00 PM

0

Bruno Desthuilliers wrote:
> Paul Rubin a écrit :
>>> ki lo wrote:
>>>> I have type variable which may have been set to 'D' or 'E'
>>>>
>>>> Now, which one of following statements are more efficient
>>>>
>>>> if type =='D' or type == 'E':
>>>>
>>>> or
>>>>
>>>> if re.search("D|E", type):
>>>>
>>>> Please let me know because the function is going to called 10s of millions
>>>> of times.
>>> For maximum efficiency you have to use a set.
>> if type in 'DE': ...
>>
>> might be faster.
>
> It might also be wrong if there's a possibility that type=='DE' :
> >>> "DE" in "DE"
> True
>
>
If the "type" variable really is a type, of course, then the real
solution is hardly radical: stop using the names of types and start
using the types:

>>> class D(object): pass
....
>>> type(D)
<type 'type'>
>>> D
<class '__main__.D'>
>>> d = D()
>>> type(d)
<class '__main__.D'>
>>> class E(object): pass
....
>>> e = E()
>>> o = object()
>>> for c in d, e, o:
.... print c, (type(c) in (D, E))
....
<__main__.D object at 0x7ff3150c> True
<__main__.E object at 0x7ff3156c> True
<object object at 0x7ff81478> False
>>>

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...

Steven D'Aprano

2/11/2008 10:23:00 PM

0

On Mon, 11 Feb 2008 10:41:44 -0500, Steve Holden wrote:

> If the "type" variable really is a type, of course, then the real
> solution is hardly radical: stop using the names of types and start
> using the types:

[snip example using "type(obj) in (type1, type2)"]


But if you're using actual types, you almost certainly want to allow for
subclasses as well:

isinstance(obj, tuple_of_types)


--
Steven

j.gemmell

10/7/2013 1:03:00 AM

0

On Sun, 06 Oct 2013 19:14:26 -0500, j.gemmell@ymail.com wrote:
Correction:
>For the Holy Bible says (2 Peter 2:11-22) Whereas angels, which are
>great in power and might, bring not railing accusation against them
>(Art Bulla and Joshua Gemmell) before the Lord. But these, as a
>natural brute beasts, made to be taken and destroyed, speak evil of
>the things that they understand not; and shall utterly perish in their
>own corruption; and shall receive the reward of unrighteousness, as
>they that count it pleasure to riot in the day time. Spots they are
>and blemishes, sporting themselves with their own deceivings while
>they feast with you; having eyes full of adultery, and that cannot
>cease from sin; beguiling unstable souls: an heart they have exercised
>with covetous practices; cursed children: which have forsake the right
>way, and are gone astray, following the way of Balaam the son of
>Bosor, who loved the wages of unrighteousness; but was rebuked for his
>iniquity: the dumb ass speaking with man's voice forbad the madness of
>the prophet. Theses are wells without water, clouds that are carried
>with a tempest; to whom the mist of darkness is reserved forever. For
>when they speak great swelling words of vanity, they allure through
>the lusts of the flesh, through much wantonness, those that were clean
>escaped from them who live in error. While they promise them liberty,
>they themselves are the servants of corruption: for of whom a man is
[overcome], of the same is he brought in bondage. For if after they
have escaped the pollutions of the world through the knowledge of the
Lord and Saviour Jesus Christ, they are again entangled therein, and
>overcome, the latter end is worse with them than the beginning. For
>it had been better for them not to have known the way of
>righteousness, than, after they have known it, to turn from the holy
>commandment delivered unto them. But it is happened unto them
>according to the true proverb, The Dog Is Turned To His Own Vomit
>Again; And The Sow That Was Washed To Her Wallowing In The Mire.

j.gemmell

10/7/2013 1:34:00 AM

0

On Sun, 06 Oct 2013 19:14:26 -0500, j.gemmell@ymail.com wrote:
Correction:
>
>Yes, I am the Archangel Raphael the Protector and Healer of God. That
>is why I seek to protect the honor [of] all prophets, and to heal the sick
>like you. Yes, you are sick with hate, Brother Aaron.