With a string of authors such as:
Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
Shawyer, Andrea
I would like to write a function to change every other comma into a
semi colon (thereby defining where one name ends and the next begins).
I could do it by writing a complex (and slow) procedure - but is there
a quick way of doing it using regular expressions, for example?
Many thanks. 27 3144 St***********@gmail.com wrote: With a string of authors such as:
Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen, Shawyer, Andrea
I would like to write a function to change every other comma into a semi colon (thereby defining where one name ends and the next begins).
I could do it by writing a complex (and slow) procedure - but is there a quick way of doing it using regular expressions, for example?
Many thanks.
Why not just use str_replace() intrinsic function?
$str = "Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev,
Karen, Shawyer, Andrea";
$str = str_replace(",", ";", $str);
echo $str;
Bernhard Enders. St***********@gmail.com wrote: With a string of authors such as:
Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen, Shawyer, Andrea
I would like to write a function to change every other comma into a semi colon (thereby defining where one name ends and the next begins).
I could do it by writing a complex (and slow) procedure - but is there a quick way of doing it using regular expressions, for example?
Regular expressions can be used, I'd think this is faster:
$array = explode(',', $authors);
$new_authors = '';
$i = 0;
while(!empty($array)){
$delimter = (($i % 2)==0) ? ',' : ';';
$new_authors .= array_shift($array).$delimiter;
$i++;
//or, to get an array:
//$new_authors[] = array_shift($array).','.array_shift($array);
}
If you want a regular expression (which almost certainly will be slower):
$authors = preg_replace('/([^,]*),([^,]*),/s','$1,$2;',$authors);
Similar, to get them directly in an array:
preg_match_all('/([^,]*,){2}/s',$authors,$matches);
Grtz,
--
Rik Wasmus
bgeneto wrote: Why not just use str_replace() intrinsic function
St***********@gmail.com wrote: I would like to write a function to change every other comma into a
----------------------------------------------------^^^^^
Grtz,
--
Rik Wasmus
bgeneto wrote: St***********@gmail.com wrote: With a string of authors such as:
Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen, Shawyer, Andrea
I would like to write a function to change every other comma into a semi colon (thereby defining where one name ends and the next begins).
I could do it by writing a complex (and slow) procedure - but is there a quick way of doing it using regular expressions, for example?
Many thanks.
Why not just use str_replace() intrinsic function?
$str = "Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen, Shawyer, Andrea"; $str = str_replace(",", ";", $str); echo $str;
Bernhard Enders.
Because he wants to change every *other* comma into a semi-colon (i.e.
change the second, fourth etc).
Your function would change *every* comma into a semi-colon.
I don't think you can easily do it with regex as you can't really
differentiate between comma's.
I would suggest something along the lines of:
$array = explode ( ', ', $string );
$arraycount = count($array);
$new_string = '';
for( $i=0; $i<$arraycount; $i++; ) {
if ((1&$i)) {
//$i is odd
$new_string .= $array[$i] . ', ';
}
elseif (!(1&$num)) {
//$i is even
$new_string .= $array[$i] . '; ';
}
}
$string = $new_string;
unset($new_string);
Hope this helps,
Juliette
Juliette wrote: I don't think you can easily do it with regex as you can't really differentiate between comma's. I would suggest something along the lines of:
$array = explode ( ', ', $string ); $arraycount = count($array); $new_string = '';
for( $i=0; $i<$arraycount; $i++; ) { if ((1&$i)) { //$i is odd $new_string .= $array[$i] . ', '; } elseif (!(1&$num)) { //$i is even $new_string .= $array[$i] . '; '; } } $string = $new_string; unset($new_string);
I would use temporary arrays and the implode() function, and a switch
statement instead of the if/elseif:
<?php
$string = "Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev,
Karen, Shawyer, Andrea";
$array = explode ( ', ', $string );
$tmp = array();
$tmp2 = array();
for( $i=0; $i<count($array); $i++ ) {
switch ($i % 2) {
case 0:
$tmp2[] = trim($array[$i]);
break;
case 1:
$tmp2[] = trim($array[$i]);
$tmp[] = implode(', ',$tmp2);
$tmp2 = array();
break;
}
}
echo 'Before: ' . $string . "\n";
$string = implode('; ',$tmp);
echo 'After: ' . $string;
?>
Ken
Rik (lu************@hotmail.com) wrote:
: St***********@gmail.com wrote:
: > With a string of authors such as:
: >
: > Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
: > Shawyer, Andrea
: >
: > I would like to write a function to change every other comma into a
: > semi colon (thereby defining where one name ends and the next begins).
: >
: > I could do it by writing a complex (and slow) procedure - but is there
: > a quick way of doing it using regular expressions, for example?
: Regular expressions can be used, I'd think this is faster:
I wouldn't assume that a block of php code is faster than simply calling
preg_replace. In fact I wouldn't be surprised if it's the other way round
- preg_replace may be faster.
preg_replace maps to a function written in C that does some string
manipulations (all code written in C). All php does internally is to set
up the parameters and call that underlying function.
The block of php code, on the other hand, maps to a whole bunch of
manipulations of php data structures driven by the php interpretter, each
one of which likely involves setting up parameters and calling some
underlying function to do the work.
I would be interested to see the result of a proper comparison between the
two. St***********@gmail.com wrote: With a string of authors such as:
Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen, Shawyer, Andrea
I would like to write a function to change every other comma into a semi colon (thereby defining where one name ends and the next begins).
I could do it by writing a complex (and slow) procedure - but is there a quick way of doing it using regular expressions, for example?
Many thanks.
Something like the following should do:
$names = ereg_replace("\([^,]*,[^,]*\),", "\\1;", $names)
Thanks for all the excellent suggestions!
Best regards,
Steve
On Tue, 20 Jun 2006 14:10:48 -0800, Malcolm Dew-Jones wrote: : Regular expressions can be used, I'd think this is faster:
I wouldn't assume that a block of php code is faster than simply calling preg_replace. In fact I wouldn't be surprised if it's the other way round - preg_replace may be faster.
I would be interested to see the result of a proper comparison between the two.
Ask and ye shall receive:
I've done a small script that runs each version 5000 times (never guess
which is faster, always test). And the results are:
Function call: 0.0749678611755 seconds
PREG Replace: 0.0623338222504 seconds
So the preg version is faster (it completes in 83% of the time).
Now, there's another upside. In the way that Rik coded them, there's also
a subtle difference in the two outputs. The Function call version puts an
extra ; on the end (which would result in an empty element if exploded and
would therefore need to be tested for before creating a DB row or the like).
Also, that test version used the original authors string. If I make this
string 10 times as long, the times are as follows:
Function call: 0.81055188179 seconds
PREG Replace: 0.196034908295 seconds
So, the preg version absolutely whips the function call (completes in 24%
of the time).
As much as I'd advocate testing over guesswork, I agree with your
understanding of why it's much faster.
Hope this is interesting to someone.
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer http://www.gphpedit.org | PHP editor for Gnome 2 http://www.andyjeffries.co.uk | Personal site and photos
On Tue, 20 Jun 2006 21:41:52 +0200, Juliette wrote: Because he wants to change every *other* comma into a semi-colon (i.e. change the second, fourth etc). Your function would change *every* comma into a semi-colon.
I don't think you can easily do it with regex as you can't really differentiate between comma's.
Hi Juliette,
As Rik already posted, you certainly can do it with regexes and as I
posted, they're much faster than the function call.
There's a syntax error in your script:
for( $i=0; $i<$arraycount; $i++; ) {
should be:
for( $i=0; $i<$arraycount; $i++ ) {
And a bug that actually gives the incorrect output. The first part of the
string from your function is:
Carson; David, Milne; Rebecca,
And it should be:
Carson, David; Milne, Rebecca;
For reference, against the larger data set from my timing post, your
function completes in 0.5357 seconds (compared to 0.8106 for Rik's
function version and 0.1960 for Rik's regex).
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer http://www.gphpedit.org | PHP editor for Gnome 2 http://www.andyjeffries.co.uk | Personal site and photos
On Wed, 21 Jun 2006 08:09:39 +0000, Andy Jeffries wrote: And a bug that actually gives the incorrect output. The first part of the string from your function is:
Carson; David, Milne; Rebecca,
And it should be:
Carson, David; Milne, Rebecca;
And it also has the same deficiency as Rik's function version in that it
adds an extra delimiter on the end.
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer http://www.gphpedit.org | PHP editor for Gnome 2 http://www.andyjeffries.co.uk | Personal site and photos
On Tue, 20 Jun 2006 13:14:20 -0700, Ken Robinson wrote: I would use temporary arrays and the implode() function, and a switch statement instead of the if/elseif:
Hi Ken,
OK, your function is the slowest of those posted so far, completing the
larger data set from my timing post in 0.9498 seconds (compared to 0.5357
seconds for Juliette, 0.8106 for Rik's function version and
0.1960 for Rik's regex).
But yours also has no errors and produces the correct output (with the
only other one being Rik's regex version).
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer http://www.gphpedit.org | PHP editor for Gnome 2 http://www.andyjeffries.co.uk | Personal site and photos
Andy Jeffries wrote: On Tue, 20 Jun 2006 14:10:48 -0800, Malcolm Dew-Jones wrote: : Regular expressions can be used, I'd think this is faster:
I wouldn't assume that a block of php code is faster than simply calling preg_replace. In fact I wouldn't be surprised if it's the other way round - preg_replace may be faster.
I would be interested to see the result of a proper comparison between the two.
[snip] Also, that test version used the original authors string. If I make this string 10 times as long, the times are as follows:
Function call: 0.81055188179 seconds PREG Replace: 0.196034908295 seconds
So, the preg version absolutely whips the function call (completes in 24% of the time).
As much as I'd advocate testing over guesswork, I agree with your understanding of why it's much faster.
Indeed preg_replace has the advantage of doing its looping in compiled code,
but throwing around with arrays is not cheap, so I think the methods are
too different to make a fair comparison.
I would think that in this case, since the logic is quite simple, doing a
straightforward character by character sweep, just like preg_replace would
do for its pattern matching, ought to be faster, unless PHP really really
*really* stinks at doing loops in phpcode of course.
Can you run the same test with this code:
$alternate_comma = false;
$length = strlen($authors);
for ($i=0; $i<$length; $i++) {
if ($authors[$i]==',') {
if ($alternate_comma) $authors[$i] = ';';
$alternate_comma = !$alternate_comma;
}
}
Usually I would use strpos for this sort of thing, which could be even
faster, since it will shorten the looping in php, but still would depend on
how well php does it.
--
/Bent
Message-ID: <pa****************************@andyjeffries.co.uk > from
Andy Jeffries contained the following: As much as I'd advocate testing over guesswork, I agree with your understanding of why it's much faster.
Hope this is interesting to someone.
It is to me, I'd always thought regexs were slower. Bugger, that means
I will /really/ have to get round to learning them.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Bent Stigsen wrote: Andy Jeffries wrote:
On Tue, 20 Jun 2006 14:10:48 -0800, Malcolm Dew-Jones wrote: Regular expressions can be used, I'd think this is faster:
I wouldn't assume that a block of php code is faster than simply calling preg_replace. In fact I wouldn't be surprised if it's the other way round - preg_replace may be faster.
I would be interested to see the result of a proper comparison between the two. [snip] Also, that test version used the original authors string. If I make this string 10 times as long, the times are as follows:
Function call: 0.81055188179 seconds PREG Replace: 0.196034908295 seconds
So, the preg version absolutely whips the function call (completes in 24% of the time).
As much as I'd advocate testing over guesswork, I agree with your understanding of why it's much faster.
Yup, I should've tested it, somtimes lazyness will make you look like a fool
:-)
Testing here, 5000 times, string 4 times as long:
Regex does 1.0872719287872
Can you run the same test with this code:
$alternate_comma = false; $length = strlen($authors); for ($i=0; $i<$length; $i++) { if ($authors[$i]==',') { if ($alternate_comma) $authors[$i] = ';'; $alternate_comma = !$alternate_comma; } }
Does it in 6.8535070419312
Usually I would use strpos for this sort of thing, which could be even faster, since it will shorten the looping in php, but still would depend on how well php does it.
strpos() example:
$a = 0;
$bool = false;
while(strpos($authors,',',$a)!== false){
$a = strpos($authors,',',$a);
if($bool) $authors[$a] = ';';
$a++;
$bool = !$bool;
}
This manages it in 2.1639029979706
Regex still seems to win this.
I thought about using array_reduce(explode(',',$authors),
'some_custom_function'), but that also doesn't even get close.
Grtz,
--
Rik Wasmus
Geoff Berrow wrote: Message-ID: <pa****************************@andyjeffries.co.uk > from Andy Jeffries contained the following:
As much as I'd advocate testing over guesswork, I agree with your understanding of why it's much faster.
Hope this is interesting to someone.
It is to me, I'd always thought regexs were slower. Bugger, that means I will /really/ have to get round to learning them.
Yup, I'm quite capable to produce some regexes, but in all testcases I saw
where it could be done without, regexes seemed slower... Time to test some
past code.
Grtz,
--
Rik Wasmus
Rik wrote: Bent Stigsen wrote: Andy Jeffries wrote:
On Tue, 20 Jun 2006 14:10:48 -0800, Malcolm Dew-Jones wrote: > Regular expressions can be used, I'd think this is faster:
I wouldn't assume that a block of php code is faster than simply calling preg_replace. In fact I wouldn't be surprised if it's the other way round - preg_replace may be faster.
I would be interested to see the result of a proper comparison between the two. [snip] Also, that test version used the original authors string. If I make this string 10 times as long, the times are as follows:
Function call: 0.81055188179 seconds PREG Replace: 0.196034908295 seconds
So, the preg version absolutely whips the function call (completes in 24% of the time).
As much as I'd advocate testing over guesswork, I agree with your understanding of why it's much faster. Yup, I should've tested it, somtimes lazyness will make you look like a fool :-)
Acceptable tradeoff :)
Testing here, 5000 times, string 4 times as long: Regex does 1.0872719287872
Can you run the same test with this code:
$alternate_comma = false; $length = strlen($authors); for ($i=0; $i<$length; $i++) { if ($authors[$i]==',') { if ($alternate_comma) $authors[$i] = ';'; $alternate_comma = !$alternate_comma; } } Does it in 6.8535070419312
Bugger. I'm a bit surprised it's slower, but that much. Useless piece of
[bleep].
Did you run it more than once. I mean, it couldn't have been some other app.
hogging the cpu, at that moment.
Usually I would use strpos for this sort of thing, which could be even faster, since it will shorten the looping in php, but still would depend on how well php does it.
strpos() example: $a = 0; $bool = false; while(strpos($authors,',',$a)!== false){ $a = strpos($authors,',',$a); if($bool) $authors[$a] = ';'; $a++; $bool = !$bool; }
This manages it in 2.1639029979706
You do strpos twice, and it can bee squeezed a bit.
Try this instead:
$a = -1;
$bool = true;
while (($a=strpos($authors, ',', $a+1))!==false)
if ($bool=!$bool) $authors[$a] = ';';
Regex still seems to win this.
There could allways be made a AlternateCommaReplacer module.
[snip]
--
/Bent
On Wed, 21 Jun 2006 13:32:50 +0200, Rik wrote: So, the preg version absolutely whips the function call (completes in 24% of the time).
As much as I'd advocate testing over guesswork, I agree with your understanding of why it's much faster. Yup, I should've tested it, somtimes lazyness will make you look like a fool :-)
I agree with your sentence, but don't think it's the case here - your
regex is still the fastest on the block! And it's provoked an interesting
discussion (and maybe given a few people reason to eventually learn
regexes).
Can you run the same test with this code:
Does it in 6.8535070419312
Using my machine (for comparison with all the previous timings):
1.8554 seconds (with correct output). Usually I would use strpos for this sort of thing, which could be even faster, since it will shorten the looping in php, but still would depend on how well php does it.
strpos() example:
This manages it in 2.1639029979706
I make it: 0.5243 seconds (within a margin of error the same as Juliette's
bit testing version, but with the correct output).
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer http://www.gphpedit.org | PHP editor for Gnome 2 http://www.andyjeffries.co.uk | Personal site and photos
On Wed, 21 Jun 2006 12:10:34 +0100, Geoff Berrow wrote: As much as I'd advocate testing over guesswork, I agree with your understanding of why it's much faster.
Hope this is interesting to someone.
It is to me, I'd always thought regexs were slower. Bugger, that means I will /really/ have to get round to learning them.
AFAIK, ereg* is slower than preg*, that may be where you got this from,
but the PCRE library is lightning fast.
I'd recommend Mastering Regular Expressions by Jeffrey Friedl (O'Reilly) -
it gets a bit mind-numbing in places (talking about the mechanics of how
different regex engines work) but it's a great reference and is quite
readable (given the subject matter).
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer http://www.gphpedit.org | PHP editor for Gnome 2 http://www.andyjeffries.co.uk | Personal site and photos
On Wed, 21 Jun 2006 13:48:00 +0200, Rik wrote: It is to me, I'd always thought regexs were slower. Bugger, that means I will /really/ have to get round to learning them.
Yup, I'm quite capable to produce some regexes, but in all testcases I saw where it could be done without, regexes seemed slower... Time to test some past code.
I always prefer them as it's generally a lot more readable (a single line
of code compared to 10 lines of iteration). In most cases developer
comprehension is more important than CPU cycles (unless you're working
with masses of data, but that's not normal in a web system).
The obvious comment coming is "regexes are more readable, what a load of
BS" - but they are if you understand regexes, and if you don't - it's not
my fault you are lacking in a skill (with "you" being the developer coming
to look at my code for maintenance).
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer http://www.gphpedit.org | PHP editor for Gnome 2 http://www.andyjeffries.co.uk | Personal site and photos
On Wed, 21 Jun 2006 15:00:41 +0200, Bent Stigsen wrote: Bugger. I'm a bit surprised it's slower, but that much. Useless piece of [bleep].
Did you run it more than once. I mean, it couldn't have been some other app. hogging the cpu, at that moment.
I've run it more than once. In my latest set of timings I get:
PREG Replace: 0.1889 seconds
Bent's Function call: 1.8074 seconds
You do strpos twice, and it can bee squeezed a bit.
Try this instead:
Bent's 2nd version Function call: 0.3320 seconds
Much faster, nearly competitive with preg and it gives the correct output.
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer http://www.gphpedit.org | PHP editor for Gnome 2 http://www.andyjeffries.co.uk | Personal site and photos
Andy Jeffries wrote: On Wed, 21 Jun 2006 08:09:39 +0000, Andy Jeffries wrote: And a bug that actually gives the incorrect output. The first part of the string from your function is:
Carson; David, Milne; Rebecca,
And it should be:
Carson, David; Milne, Rebecca;
And it also has the same deficiency as Rik's function version in that it adds an extra delimiter on the end.
Cheers,
Andy
Andy,
Oops.. I obviously posted too quickly (and yes, I didn't test the code
before posting, just did it off-hand).
Interesting to see the ensuing discussion this has brought. I normally
use preg myself, but just couldn't think of how to do this one. In that
respect I still have a nice challenge (regex) I'm working on myself
which I can't seem to get right.
Just in case someone is interested, you can find the corrected code from
my function below. I've also gotten rid of some of the 'fluff' which I
only put in to make the code easier to understand.
About the extra delimiter: that was not explicite in the requirements
given by the OP, so that's why I didn't take that into account. The
below function does however deal with that correctly, i.e. the way you
suggested it ought to work.
$array = explode ( ', ', $string );
$arraycount = ( count($array)-1 );
$string = '';
for( $i=0; $i<=$arraycount; $i++ ) {
$string .= ( $i === $arraycount ) ? ( $array[$i] ) : ( (1&$i) ? (
$array[$i] . '; ' ) : ( $array[$i] . ', ' ) );
}
unset( $array, $arraycount );
The regex will still win, but I would guess this is slightly faster than
my earlier function.
Grz, Juliette
On Wed, 21 Jun 2006 20:17:25 +0200, Juliette wrote: Oops.. I obviously posted too quickly (and yes, I didn't test the code before posting, just did it off-hand).
I tend to do the same thing, it's only because I have a particular
interest in optimising functions/code that I took an interest in this
thread.
Interesting to see the ensuing discussion this has brought. I normally use preg myself, but just couldn't think of how to do this one. In that respect I still have a nice challenge (regex) I'm working on myself which I can't seem to get right.
That's a good point, I think Rik's regex is probably optimal (and indeed
that's the way I'd have done it) but is there a faster regex?
Just in case someone is interested, you can find the corrected code from my function below. I've also gotten rid of some of the 'fluff' which I only put in to make the code easier to understand. About the extra delimiter: that was not explicite in the requirements given by the OP, so that's why I didn't take that into account. The below function does however deal with that correctly, i.e. the way you suggested it ought to work.
Cool, tested - result below :-)
The regex will still win, but I would guess this is slightly faster than my earlier function.
Indeed it is, this function runs in 0.4208 seconds making it the second
fasted non-regex function.
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer http://www.gphpedit.org | PHP editor for Gnome 2 http://www.andyjeffries.co.uk | Personal site and photos
Andy Jeffries wrote: On Wed, 21 Jun 2006 20:17:25 +0200, Juliette wrote: The regex will still win, but I would guess this is slightly faster than my earlier function.
Indeed it is, this function runs in 0.4208 seconds making it the second fasted non-regex function.
Cool ! ;-)
I've looked at Andy's to see what he does and bleeping heck, that's a
hard one to get my head round, but I can see why that would be faster.
Changing my function to a while loop would probably take yet some more
of the timing, but I haven't really got the time to do so now.
Regex wins the show.
Grz, Juliette
Juliette wrote: Andy Jeffries wrote: On Wed, 21 Jun 2006 20:17:25 +0200, Juliette wrote: The regex will still win, but I would guess this is slightly faster than my earlier function. Indeed it is, this function runs in 0.4208 seconds making it the second fasted non-regex function.
Thanks for doing all the stats so far, hope you have time for two more :)
Cool ! ;-) I've looked at Andy's to see what he does and bleeping heck, that's a hard one to get my head round, but I can see why that would be faster.
I think you can save a few jiffies by skipping the last entry in the array,
and just add it in the end. Saves you a comparison in the loop.
Like:
$array = explode ( ', ', $string );
$arraycount = ( count($array)-1 );
$string = '';
for( $i=0; $i<$arraycount; $i++ ) {
$string .= ((1&$i) ? ( $array[$i] . '; ' ) : ( $array[$i] . ', ' ));
}
$string .= $array[$i];
Changing my function to a while loop would probably take yet some more of the timing, but I haven't really got the time to do so now.
$array = explode ( ', ', $string );
$string = current($array);
$x1 = null;
$x2 = next($array);
while ($x1=next($array)) {
$string .= ', '. $x2 . '; ' . $x1;
$x2 = next($array);
}
$string .= ', ' . $x2;
So many ways. The thread should have been called "50 ways to change..."
--
/Bent
On Wed, 21 Jun 2006 21:39:26 +0200, Bent Stigsen wrote: On Wed, 21 Jun 2006 20:17:25 +0200, Juliette wrote: The regex will still win, but I would guess this is slightly faster than my earlier function.
Indeed it is, this function runs in 0.4208 seconds making it the second fasted non-regex function. Thanks for doing all the stats so far, hope you have time for two more :)
Sue :-)
I think you can save a few jiffies by skipping the last entry in the array, and just add it in the end. Saves you a comparison in the loop.
Like:
$array = explode ( ', ', $string ); $arraycount = ( count($array)-1 ); $string = ''; for( $i=0; $i<$arraycount; $i++ ) { $string .= ((1&$i) ? ( $array[$i] . '; ' ) : ( $array[$i] . ', ' )); } $string .= $array[$i];
OK, you had the fastest non-regex function before this one.... and....
this one is slower (0.3638 seconds).
Changing my function to a while loop would probably take yet some more of the timing, but I haven't really got the time to do so now.
And the while loop version takes 0.3587 seconds. So your 2nd non-regex
version still stands as the fastest non-regex version.
So many ways. The thread should have been called "50 ways to change..."
LOL! :-)
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer http://www.gphpedit.org | PHP editor for Gnome 2 http://www.andyjeffries.co.uk | Personal site and photos
Andy Jeffries wrote: On Wed, 21 Jun 2006 13:48:00 +0200, Rik wrote: It is to me, I'd always thought regexs were slower. Bugger, that means I will /really/ have to get round to learning them. Yup, I'm quite capable to produce some regexes, but in all testcases I saw where it could be done without, regexes seemed slower... Time to test some past code.
I always prefer them as it's generally a lot more readable (a single line of code compared to 10 lines of iteration). In most cases developer comprehension is more important than CPU cycles (unless you're working with masses of data, but that's not normal in a web system).
It's true that most often it's not the bottleneck. Opening files/databases
etc. are usually terrible more consuming (hey, there is a reason why we had
to loop this code 5000 times to get a reasonable result to compare.
Nevertheless, iot never hurts to make is as efficient as possible.
The obvious comment coming is "regexes are more readable, what a load of BS" - but they are if you understand regexes, and if you don't - it's not my fault you are lacking in a skill (with "you" being the developer coming to look at my code for maintenance).
Regexes can be perfectly commented, which is rarely used unfortunately.
Using /x to newline/indent (and thus make a regex more readable) should also
be used more often. Then, instead of 1 line >400 characters long with some
code you've got a nice regex that you comprehend in a lot less time. No more
searching for the opening or closing parenthesis.
Grtz,
--
Rik Wasmus This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: techie |
last post by:
Hi All,
I have a portal working on an intranet for a company. The portal was
designed keeping in mind the resolution of 1024*768. I am including a
..css file in all the files. Now i have to make...
|
by: bigoxygen |
last post by:
Hi.
I have a list that is similar to this:
-Evaluation
+test
+test
-Students
+test
I would like to change the "-" bullet only, but I cannot. My
|
by: Bryan R. Meyer |
last post by:
I am a relatively new C++ programmer and am attempting to write a
function that takes a number of type float and adds commas to it in
the appropriate places. In order to manipulate the number to...
|
by: Alan Lane |
last post by:
Hello world:
I have a ListBox that I fill as a ValueList from a SQL query. The SQL looks
like:
SELECT Format(COST,"$#,000") As ItemCost FROM tblPricing ...
I put semicolons between each...
|
by: Grant Robertson |
last post by:
I am relatively new to XML, though I have taught myself a lot in the past
month. I keep seeing namespace names with multiple colons in them such
as:
"urn:oasis:names:tc:entity:xmlns:xml:catalog"...
|
by: Adrienne Boswell |
last post by:
Although this is a client side issue, I am also posting to asp.general
in case there is someway to do this only server side (which I would
prefer).
Here's my form:
<form method="post"...
|
by: nigel |
last post by:
Hi,
I'm using VBA to export data from a table direct to a CSV file
DoCmd.TransferText acExportDelim, , "ExportTable", filePath
this produced a file with COMMA separated values,...
|
by: machineghost |
last post by:
First off, let me just say that as someone with no DBA training
whatsoever, any help I can get with this issue will be very, very much
appreciated.
My company recently migrated our database from...
|
by: Frank Swarbrick |
last post by:
>>On 6/27/2008 at 7:00 PM, in message
<fe715a94-6138-4934-9ff7-aa4e4d2f5e7e@c19g2000prf.googlegroups.com>,
machineghost<machineghost@gmail.comwrote:
Not to be offensive, but...did you try it?
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |