473,609 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String functions: what's the difference?

(absolute beginner here, sorry if this seems basic)

Section 7.10 of 'How to Think Like a Computer Scientist' contains this
discussion of string.find and other string functions:

(quote)
We can use these constants and find to classify characters. For example, if
find(lowercase, ch) returns a value other than -1, then ch must be lowercase:

def isLower(ch):
return string.find(str ing.lowercase, ch) != -1

Alternatively, we can take advantage of the in operator, which determines
whether a character appears in a string:
def isLower(ch):
return ch in string.lowercas e

As yet another alternative, we can use the comparison operator:
def isLower(ch):
return 'a' <= ch <= 'z'
If ch is between a and z, it must be a lowercase letter.

As an exercise, discuss which version of isLower you think will be
fastest. Can you think of other reasons besides speed to prefer one
or the other?

(end quote)

I've tried all three, but the function is so small (test a single letter) I
can't measure the difference. I'm using time.time() to see how long it takes to
execute the function.
I could use a loop to increase execution time, but then I might be measuring
mostly overhead.

I'd expect the third option to be the fastest (involves looking up 3 values,
where the others have to iterate through a-z), but am I right?
And reasons to prefer one? a-z doesn't contain all lowercase letters (it omits
acents and symbols), but other than that?
--
Harro de Jong
remove the extra Xs from xmsnet to mail me
Mar 9 '06 #1
4 1874
Harro de Jong wrote:
I've tried all three, but the function is so small (test a single letter) I
can't measure the difference. I'm using time.time() to see how long it takes to
execute the function.
I could use a loop to increase execution time, but then I might be measuring
mostly overhead.
Still, this is what you should do. Try the timeit.py module; it does the
loop for you. Surprisingly, one of the faster ways to do a loop is

nones = [None]*10000000

<start timer>
for x in nones:
<action>
<stop timer>

This is fast because no Python integers are created to implement the
loop.
I'd expect the third option to be the fastest (involves looking up 3 values,
where the others have to iterate through a-z), but am I right?
Just measure it for yourself. I just did, and the third option indeed
came out fastest, with the "in" operator only slightly slower.
And reasons to prefer one?


For what purpose? To find out whether a letter is lower-case?

Just use the .islower() method on the character for that.

Regards,
Martin
Mar 9 '06 #2
gry
First, don't appologize for asking questions. You read, you thought,
and you tested. That's more than many people on this list do. Bravo!

One suggestion: when asking questions here it's a good idea to always
briefly mention which version of python and what platform (linux,
windows, etc) you're using. It helps us answer your questions more
effectively.

For testing performance the "timeit" module is great. Try something
like:
python -mtimeit -s 'import string;from myfile import isLower'
"isLower('x ')"

You didn't mention the test data, i.e. the character you're feeding to
isLower.
It might make a difference if the character is near the beginning or
end of the range.

As to reasons to prefer one or another implementation, one *very*
important question is "which one is clearer?". It may sound like a
minor thing, but when I'm accosted first thing in the
morning(pre-coffee) about a nasty urgent bug and sit down to pore over
code and face "string.find(st ring.lowercase, ch) != -1", I'm not happy.

Have fun with python!
-- George Young

Mar 9 '06 #3
<gr*@ll.mit.edu > wrote:

One suggestion: when asking questions here it's a good idea to always
briefly mention which version of python and what platform (linux,
windows, etc) you're using.
Of course, forgot about that. It's Python 2.4.2 for Windows.

For testing performance the "timeit" module is great. Try something
like:
python -mtimeit -s 'import string;from myfile import isLower'
"isLower('x ')"
Thanks for the pointer. I was using time.time(), which I now see isn't
very accurate on Windows.

You didn't mention the test data, i.e. the character you're feeding to
isLower.
It might make a difference if the character is near the beginning or
end of the range.
(slaps forehead) I used "A" as the input, of course that would make a
difference.

....
Have fun with python!


If the few days I've spent on it so far are any indication, I will. This
is my first foray into programming since college; I like 'How to Think
Like a Computer Scientist' much better than my impenetrable C textbook.

--
Harro de Jong
remove the extra Xs from xmsnet to mail me
Mar 9 '06 #4
Harro de Jong wrote:
Thanks for the pointer. I was using time.time(), which I now see isn't
very accurate on Windows.


time.clock() is more accurate on Windows (and much less so on
Linux, where it also measures something completely different.)
Mar 9 '06 #5

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

Similar topics

99
5876
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
18
7815
by: Metro12 | last post by:
In the <basic_string.h>, I find the implementation of these two functions. But I can't understand the difference between them. Please give me some help! //basic_string::c_str() const _CharT* c_str() const { // MT: This assumes concurrent writes are OK. size_type __n = this->size(); traits_type::assign(_M_data(), _Rep::_S_terminal);
9
5693
by: Danny | last post by:
HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this<br>is<br>a<br>test would return 3 for <br>
26
563
by: junky_fellow | last post by:
Consider the following piece of code: char *str = "Hello"; if (str = "Hello") printf("\nstring matches\n"); str is pointer to char and "Hello" is a string literal whose type is "array of char". How can we compare two different objects for equality ? Is some conversion is being done before that comparison ?
3
21409
by: Mark Kamoski | last post by:
Hi-- What is the difference between Convert.ToString(obj) and CType(obj, String)? (Assume obj is a variable of type Object.) Please advise. Thank you.
35
2555
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C# newsgroup on 25 September. The regular expressions where in that newsgroup too involved. I told yesterday night, to Jay that I would test all 4 methods and the stupid method I was thinking of the first time that night when I saw Jon's
12
7189
by: CMirandaman | last post by:
Sounds like a stupid question I know. I can tell that they are used to copy strings. But what is the difference between x = y; versus x = String.Copy(y); Or are they essentially the same?
87
5094
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for the library, and other resources related to managed strings are available for download from the CERT web site at: http://www.cert.org/secure-coding/managedstring.html
92
3291
by: =?Utf-8?B?bW9iaWxlbW9iaWxl?= | last post by:
I'm trying to load this structure for a call to DeviceIoControl: typedef struct _NDISUIO_QUERY_OID { NDIS_OID Oid; PTCHAR ptcDeviceName; UCHAR Data; } NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID; I created the equivalent in VB:
0
8053
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,...
0
8557
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8513
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8380
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
6047
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
4007
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2519
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
1
1638
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.