Re: best way to replace first word in string?

2005-10-23 Thread Steven D'Aprano
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

Re: best way to replace first word in string?

2005-10-22 Thread Steven D'Aprano
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:

Re: best way to replace first word in string?

2005-10-22 Thread Steven D'Aprano
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 %. --

Re: best way to replace first word in string?

2005-10-22 Thread Chris F.A. Johnson
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

Re: best way to replace first word in string?

2005-10-22 Thread Mike Meyer
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 =

Re: best way to replace first word in string?

2005-10-22 Thread Ron Adam
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/)

Re: best way to replace first word in string?

2005-10-22 Thread [EMAIL PROTECTED]
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

Re: best way to replace first word in string?

2005-10-22 Thread William Park
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

Re: best way to replace first word in string?

2005-10-22 Thread Chris F.A. Johnson
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

Re: best way to replace first word in string?

2005-10-22 Thread Steven D'Aprano
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

Re: best way to replace first word in string?

2005-10-22 Thread Steven D'Aprano
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

Re: best way to replace first word in string?

2005-10-22 Thread Ron Adam
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

Re: best way to replace first word in string?

2005-10-22 Thread [EMAIL PROTECTED]
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

Re: best way to replace first word in string?

2005-10-22 Thread Ron Adam
[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

Re: best way to replace first word in string?

2005-10-22 Thread Mike Meyer
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

Re: best way to replace first word in string?

2005-10-21 Thread Steven D'Aprano
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

Re: best way to replace first word in string?

2005-10-21 Thread William Park
[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

best way to replace first word in string?

2005-10-20 Thread [EMAIL PROTECTED]
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 --

Re: best way to replace first word in string?

2005-10-20 Thread Larry Bates
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 %

Re: best way to replace first word in string?

2005-10-20 Thread Micah Elliott
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

Re: best way to replace first word in string?

2005-10-20 Thread Mike Meyer
[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

Re: best way to replace first word in string?

2005-10-20 Thread Fredrik Lundh
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

Re: best way to replace first word in string?

2005-10-20 Thread Hagai Cohen
Realy Thanks, I will try this Hagai -- http://mail.python.org/mailman/listinfo/python-list