473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Most efficient way to evaluate the contents of a variable.

Hey, I'm new to python and am looking for the most efficient way to
see if the contents of a variable is equal to one of many options.

Cheers,
Bryce R

Jul 13 '07 #1
2 1066
bdude schrieb:
Hey, I'm new to python and am looking for the most efficient way to
see if the contents of a variable is equal to one of many options.

Cheers,
Bryce R
if var in ('-h', '--hello', '-w', '--world'):
pass

Most maintenance-efficient, that is.
Jul 13 '07 #2
On Fri, 13 Jul 2007 06:37:19 +0200, Stargaming wrote
bdude schrieb:
Hey, I'm new to python and am looking for the most efficient way to
see if the contents of a variable is equal to one of many options.

Cheers,
Bryce R

if var in ('-h', '--hello', '-w', '--world'):
pass
Unless the list of choices is trivially small, a set performs membership tests
faster than a tuple:

$ python -m timeit -s "s=('eggs','spam','parrot')" "'eggs' in s"
10000000 loops, best of 3: 0.177 usec per loop

$ python -m timeit -s "s=('eggs','spam','parrot')" "'spam' in s"
1000000 loops, best of 3: 0.219 usec per loop

$ python -m timeit -s "s=('eggs','spam','parrot')" "'parrot' in s"
1000000 loops, best of 3: 0.262 usec per loop

$ python -m timeit -s "s=('eggs','spam','parrot')" "'python' in s"
1000000 loops, best of 3: 0.303 usec per loop

$ python -m timeit -s "s=set(('eggs','spam','parrot'))" "'eggs' in s"
10000000 loops, best of 3: 0.192 usec per loop

$ python -m timeit -s "s=set(('eggs','spam','parrot'))" "'spam' in s"
10000000 loops, best of 3: 0.192 usec per loop

$ python -m timeit -s "s=set(('eggs','spam','parrot'))" "'parrot' in s"
10000000 loops, best of 3: 0.192 usec per loop

$ python -m timeit -s "s=set(('eggs','spam','parrot'))" "'python' in s"
10000000 loops, best of 3: 0.189 usec per loop

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a...
1
by: Christoph Putz | last post by:
Hi! How can i do somewhat like saxon:evaluate in my xslt transformation? I tried to write an extension object, but in my case i need some context information (the value of an xslt-variable) to...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
2
by: Ryan McLean | last post by:
Hello everyone, I have a question for you. If I need to print 20,000 lines to a file which option is the most efficient? (these are generated inside a datareader loop) #1 Concatenate the data...
10
by: storyGerald | last post by:
Recently, I'm interested in writing very efficient code. Problem: there is a sequence { a(0), a(1), ..., a(n-1) } and a very small positive integer k, write an algorithm without using multiply...
8
by: No Such Luck | last post by:
Is there anyway to literally evaluate the contents of a string in an if statement? For example: int i = 0; char * str = "i == 0"; if(str) /* I know this doesn't do what I want */ {
15
by: Gan Quan | last post by:
I'm writing a c++ program that has many (100+) threads read/write files simultaneously. It works well if not considering the efficiency. The file i/o seems to be the bottleneck. This is my code...
1
by: shellon | last post by:
Hi all: I met a problem when using document.evaluate() to get text content using XPath, my code is as follows: nodes = document.evaluate("/html/body/div/ul/li", document, null,XPathResult....
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.