473,804 Members | 3,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What algorithm does Python use to evaluate: if substring in string

I would be surprised if it is the naive:

m = 0
s1 = "me"
s2 = "locate me"
s1len = len(s1)
s2len = len(s2)
found = False

while m + s1len <= s2len:
if s1 == s2len[m:m+s1len]:
found = True
break
m += 1
Sep 9 '06 #1
5 2887
Tor Erik <to*******@gmai l.comwrote:
I would be surprised if it is the naive:
Yep -- it's "a mix between Boyer-Moore and Horspool with a few more
bells and whistles on the top", as documented and implemented in
Objects/stringlib/fastsearch.h in the Python sources and well discussed
and explained at http://effbot.org/zone/stringlib.htm .
Alex

Sep 9 '06 #2
In <ed**********@n ews.uit.no>, Tor Erik wrote:
I would be surprised if it is the naive:
Why?

I guess it simply calls an appropriate C library function.

Ciao,
Marc 'BlackJack' Rintsch
Sep 9 '06 #3
Alex Martelli wrote:
Tor Erik <to*******@gmai l.comwrote:
>I would be surprised if it is the naive:

Yep -- it's "a mix between Boyer-Moore and Horspool with a few more
bells and whistles on the top", as documented and implemented in
Objects/stringlib/fastsearch.h in the Python sources and well discussed
and explained at http://effbot.org/zone/stringlib.htm .
Alex
Ok. Two questions:

1. Is "a in b" simply an alias for "b.find(a)" ?

2. Is this algorithm exclusive to Python 2.5, or is it contained in 2.4
aswell?
Sep 9 '06 #4
Tor Erik <to*******@gmai l.comwrote:
Alex Martelli wrote:
Tor Erik <to*******@gmai l.comwrote:
I would be surprised if it is the naive:
Yep -- it's "a mix between Boyer-Moore and Horspool with a few more
bells and whistles on the top", as documented and implemented in
Objects/stringlib/fastsearch.h in the Python sources and well discussed
and explained at http://effbot.org/zone/stringlib.htm .
Alex

Ok. Two questions:

1. Is "a in b" simply an alias for "b.find(a)" ?
The 'in' operator can be minutely better optimized, but they share the
underlying algorithm (in 2.5).
2. Is this algorithm exclusive to Python 2.5, or is it contained in 2.4
aswell?
It's 2.5 novelty. Look at the performance on the same machine (my 2.0
GHz MBP, MacOSX 10.4.7):

brain:~ alex$ python2.4 -mtimeit -s'x="foo";y="ba r"*99+x+"baz"*7 7' 'x in
y'
100000 loops, best of 3: 9.04 usec per loop
brain:~ alex$ python2.4 -mtimeit -s'x="foo";y="ba r"*99+x+"baz"*7 7'
'y.find(x)!=-1'
100000 loops, best of 3: 2.01 usec per loop
brain:~ alex$ python2.5 -mtimeit -s'x="foo";y="ba r"*99+x+"baz"*7 7' 'x in
y'1000000 loops, best of 3: 0.452 usec per loop
brain:~ alex$ python2.5 -mtimeit -s'x="foo";y="ba r"*99+x+"baz"*7 7'
'y.find(x)!=-1'
1000000 loops, best of 3: 0.842 usec per loop

find used to be way faster than 'in' -- now they share algorithms and
'in' can be more optimized (no need to track ``where'' it finds a match,
so to speak;-), so find is over twice as fast as it used to be, 'in' is
about 20 times as fast as it used to be, in this example -- it gets even
better if you look at larger and larger strings, e.g...:

brain:~ alex$ python2.4 -mtimeit -s'x="foo"*123;y ="bar"*999+x+"b az"*777'
'x in y'
10000 loops, best of 3: 91.9 usec per loop
brain:~ alex$ python2.5 -mtimeit -s'x="foo"*123;y ="bar"*999+x+"b az"*777'
'x in y'
100000 loops, best of 3: 3.01 usec per loop

here, we're going _30_ times as fast, not "just" 20;-).
Alex
Sep 9 '06 #5
"Tor Erik" wrote:
I would be surprised if it is the naive:
2.4 and earlier uses that algorithm (but with a better implementation) .

And "naive" isn't really the right word here; on average, a brute force search is pretty
good for the find/index/in use case. Most fancy algorithms ignore the setup costs and
other overhead, which works well in theory, but not that well in practice.

</F>

Sep 11 '06 #6

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

Similar topics

9
7662
by: Kim | last post by:
Hi everyone, I'm writing a embeded python program, and I want to evaluate some expression by calling function: PyRun_SimpleString("print 'hello'") I don't want to output it to stdout but putting it into string somehow sothat I can process it. Do anybody know how I can do that?
16
2666
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects with the same A or B value. But there can be more than one object with A = null or B = null in the bucket. Sometimes there is only one valid solution, sometimes there are more valid solutions, and sometimes there isn't a complete solution at...
3
12976
by: jose luis fernandez diaz | last post by:
Hi, Does any know a algorithm to strip heading and leading blank in a string ? Tnansk, Jose Luis
46
4266
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do believe MSFT should do to improve C#, however. I know that in the "Whidbey" release of VS.NET currently
18
1761
by: Jm | last post by:
Hi all I feel stupid for asking this, but i just went to use the left() function from vb6 only to find it doesnt do what it used to under .NET. Im assuming theres something else now im meant to do when i want to say for example Dim Str as string Str = "1234" msgbox left(str,2)
23
3652
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a so-called pre-fix notation or algebraic notation. Algebraic notations have the concept of operators, meaning, symbols placed around arguments. In algebraic in-fix notation, different
4
1401
by: =?Utf-8?B?Ym9va2VyQG1ndA==?= | last post by:
Ok, I inherited some code written in vb that is part of a web application. My overall objective is to be able to take multiple names from a "LastName" text box and use those names in my SQL query against my database. Currently the way it is coded, the text box will pass one name only to the next page, which then gets formed into the SQL query. I will provide some examples of the code that is used to perform this task... Ok, when you...
1
2141
by: sourab123 | last post by:
hello i got frustrated after making several tries to make the minimax algorithm work in my console tic tac toe. the problem is the algorithm doesn't play the game as it should.The thing is it just tries to play the move that it 'sees' to be the best for itself without caring that its opponet is building an easy three to win the game. For ex. it playing as second player as ist player row=0;col=0; minimax player row=2;col=1; ist player...
16
4527
by: raylopez99 | last post by:
I am running out of printing paper trying to debug this...it has to be trivial, but I cannot figure it out--can you? Why am I not printing text, but just the initial string "howdy"? On the screen, when I open a file, the entire contents of the file is in fact being shown...so why can't I print it later? All of this code I am getting from a book (Chris Sells) and the net. The solution is to be found in the fact that stringbuilder is...
0
9575
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
10564
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...
1
10308
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
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
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
6846
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
5513
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
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3806
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.