473,805 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1079
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','spa m','parrot')" "'eggs' in s"
10000000 loops, best of 3: 0.177 usec per loop

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

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

$ python -m timeit -s "s=('eggs','spa m','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
21213
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 native English speaker, it's quite hard to make the problem clear. Please see the following example.
1
1924
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 evaluate the xpath expression. Is there a way to get some context information into an extension object?
11
3628
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 million lines. b) I need to store the contents into a unique hash. c) I need to then sort the data on a specific field. d) I need to pull out certain fields and report them to the user.
2
3731
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 into a string variable, then write file at the end of the loop (99.9% sure this is not it) #2 Append the data into stringbuilder variable, then write file at the end of the loop #3 Append the data into stringbuilder variable, writing to the file
10
1394
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 to calculate the following formula: n-1 _____ \ \ ki
8
13130
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
3660
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 to read from and write to files: #include <fstream> #include <sstream> #include <string>
1
2663
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. UNORDERED_NODE_SNAPSHOT_TYPE , null); in the XPath expression "/html/body/div/ul/li", I need to traverse from li to li,
21
2028
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 attributes (colls in a data base/ csv file, eg. height weight income etc.) to determine the most efficient 'type' to convert the attribute coll into for further processing and efficient storage... Example row from sampled file data: , ....]
0
9718
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.