On Sun, 23 Oct 2005 01:30:36 -0400, Mike Meyer wrote:
At least, that's what it looks like to me -- I'm perplexed by the *vast*
increase in speed in your version, far more than I would have predicted
from pulling out the char conversion. I can think of three
possibilities:
Everything got
On Thu, 20 Oct 2005 10:25:27 -0700, Micah Elliott wrote:
I thought that string concatenation was rather
expensive, so its being faster than %-formatting surprised me a bit:
Think about what string concatenation actually does:
s = hello + world
In pseudo-code, it does something like this:
On Sat, 22 Oct 2005 21:05:43 +1000, Steven D'Aprano wrote:
The thing is, a
*single* string concatenation is almost certainly more efficient than a
single string concatenation.
Dagnabit, I meant a single string concatenation is more efficient than a
single string replacement using %.
--
On 2005-10-22, William Park wrote:
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
I am looking for the best and efficient way to replace the first word
in a str, like this:
aa to become - /aa/ to become
I know I can use spilt and than join them
but I can also use regular expressions
and I sure
Steven D'Aprano [EMAIL PROTECTED] writes:
py def tester(n):
... s1 =
... s2 = %s * n
... bytes = tuple([chr(i % 256) for i in range(n)])
... t1 = time.time()
... for i in range(n):
... s1 = s1 + chr(i % 256)
... t1 = time.time() - t1
... t2 =
Steven D'Aprano wrote:
def replace_word(source, newword):
Replace the first word of source with newword.
return newword + + .join(source.split(None, 1)[1:])
import time
def test():
t = time.time()
for i in range(1):
s = replace_word(aa to become, /aa/)
The RE way, was much slower
I used the spilt, it was better
I tought because there was no need to take it to the memory again, but
it just my thougth
--
http://mail.python.org/mailman/listinfo/python-list
Chris F.A. Johnson [EMAIL PROTECTED] wrote:
On 2005-10-22, William Park wrote:
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
I am looking for the best and efficient way to replace the first word
in a str, like this:
aa to become - /aa/ to become
I know I can use spilt and than join them
On 2005-10-22, William Park wrote:
Chris F.A. Johnson [EMAIL PROTECTED] wrote:
On 2005-10-22, William Park wrote:
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
I am looking for the best and efficient way to replace the first word
in a str, like this:
aa to become - /aa/ to become
I know I
On Sat, 22 Oct 2005 21:41:58 +, Ron Adam wrote:
Don't forget a string can be sliced. In this case testing before you
leap is a win. ;-)
Not much of a win: only a factor of two, and unlikely to hold in all
cases. Imagine trying it on *really long* strings with the first space
close to
On Sat, 22 Oct 2005 14:54:24 -0400, Mike Meyer wrote:
The string formatting is two orders of magnitude faster than the
concatenation. The speed difference becomes even more obvious when you
increase the number of strings being concatenated:
The test isn't right - the addition test case
Steven D'Aprano wrote:
On Sat, 22 Oct 2005 21:41:58 +, Ron Adam wrote:
Don't forget a string can be sliced. In this case testing before you
leap is a win. ;-)
Not much of a win: only a factor of two, and unlikely to hold in all
cases. Imagine trying it on *really long* strings
interesting. seems that if ' ' in source: is a highly optimized code
as it is even faster than if str.find(' ') != -1:' when I assume they
end up in the same C loops ?
Ron Adam wrote:
Guess again... Is this the results below what you were expecting?
Notice the join adds a space to the end if
[EMAIL PROTECTED] wrote:
interesting. seems that if ' ' in source: is a highly optimized code
as it is even faster than if str.find(' ') != -1:' when I assume they
end up in the same C loops ?
The 'in' version doesn't call a function and has a simpler compare. I
would think both of those
Steven D'Aprano [EMAIL PROTECTED] writes:
On Sat, 22 Oct 2005 14:54:24 -0400, Mike Meyer wrote:
The string formatting is two orders of magnitude faster than the
concatenation. The speed difference becomes even more obvious when you
increase the number of strings being concatenated:
The test
On Thu, 20 Oct 2005 08:26:43 -0700, [EMAIL PROTECTED] wrote:
I am looking for the best and efficient way to replace the first word
in a str, like this:
aa to become - /aa/ to become
I know I can use spilt and than join them
but I can also use regular expressions
and I sure there is a lot
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
I am looking for the best and efficient way to replace the first word
in a str, like this:
aa to become - /aa/ to become
I know I can use spilt and than join them
but I can also use regular expressions
and I sure there is a lot ways, but I need
I am looking for the best and efficient way to replace the first word
in a str, like this:
aa to become - /aa/ to become
I know I can use spilt and than join them
but I can also use regular expressions
and I sure there is a lot ways, but I need realy efficient one
--
There is a gotcha on this:
How do you define word? (e.g. can the
first word be followed by space, comma, period,
or other punctuation or is it always a space).
If it is always a space then this will be pretty
efficient.
string=aa to become
firstword, restwords=s.split(' ',1)
newstring=/%s/ %s %
On Oct 20, [EMAIL PROTECTED] wrote:
I am looking for the best and efficient way to replace the first word
in a str, like this:
aa to become - /aa/ to become
I know I can use spilt and than join them
but I can also use regular expressions
and I sure there is a lot ways, but I need realy
[EMAIL PROTECTED] [EMAIL PROTECTED] writes:
I am looking for the best and efficient way to replace the first word
in a str, like this:
aa to become - /aa/ to become
I know I can use spilt and than join them
but I can also use regular expressions
and I sure there is a lot ways, but I need
Micah Elliott wrote:
And the regex is comparatively slow, though I'm not confident this one
is optimally written:
$ python -mtimeit -s'import re' '
re.sub(r^(\w*), r/\1/, a b c)'
1 loops, best of 3: 44.1 usec per loop
the above has to look the pattern up in the
Realy Thanks, I will try this
Hagai
--
http://mail.python.org/mailman/listinfo/python-list
23 matches
Mail list logo