Trinary operator?
Cliff Wells
logiplexsoftware at earthlink.net
Fri Apr 19 12:15:52 EDT 2002
On Fri, 19 Apr 2002 10:37:23 +0100
Dale Strickland-Clark wrote:
> >
> >Or it might be preferable to use a dictionary:
>
> But several orders of magnitude more processing. Certainly not for
> tight, efficient loops.
>
It does take considerably longer, but longer is still relative:
import time
gender = 'f'
t = time.time()
for i in range(100000):
verboseGender = (gender == 'm') and 'male' or 'female'
print time.time() - t
t = time.time()
for i in range(100000):
verboseGender = {'m': 'male', 'f': 'female'}.get(gender, 'unknown')
print time.time() - t
0.178238987923
0.567307949066
So for 100,000 iterations, and/or takes around .2s and a dict takes around .6s
(PIII 800MHz). I'd still weigh in favor of the dict for most applications,
although there may be situations where this would make a difference.
Regards,
--
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308 (800) 735-0555 x308
More information about the Python-list
mailing list