473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

speed questions

say i was trying to append a % before any occurance of either abc,
abd, or abe, in a string. which would be faster?:

$string = str_replace("abc","%abc",$string);
$string = str_replace("abd","%abd",$string);
$string = str_replace("abe","%abe",$string);

or

$string - preg_replace("/(abc|abd|abe)/","%$1",$string);

php.net says that preg_replace is slower than one str_replace, but for
multiple ones, it seems like it would gain a slight speed advantage.

also, exactly how would the following to code segments compare, speed
wise?

for ($num=0;$num<$length;$num++) {
$array1[] = $num;
$array2[] = $num;
}

for ($num=0;$num<$length;$num++)
$array1[] = $num;
for ($num=0;$num<$length;$num++)
$array2[] = $num;

it seems like the first one should require 3*$length operations,
whereas the first one would require 4*$length operations, although i'm
not 100% sure on that.
Jul 17 '05 #1
4 1595
yawnmoth wrote:
say i was trying to append a % before any occurance of either abc,
abd, or abe, in a string.**which*would*be*faster?:

$string = str_replace("abc","%abc",$string);
$string = str_replace("abd","%abd",$string);
$string = str_replace("abe","%abe",$string);

or

$string - preg_replace("/(abc|abd|abe)/","%$1",$string);


Write a test script and run it in a loop say 1000 times doing the firstand
then the second and measure it. That'll tell you which is faster.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2
"yawnmoth" <te*******@yahoo.com> wrote in message
news:7j********************************@4ax.com...
say i was trying to append a % before any occurance of either abc,
abd, or abe, in a string. which would be faster?:

$string = str_replace("abc","%abc",$string);
$string = str_replace("abd","%abd",$string);
$string = str_replace("abe","%abe",$string);

or

$string - preg_replace("/(abc|abd|abe)/","%$1",$string);

php.net says that preg_replace is slower than one str_replace, but for
multiple ones, it seems like it would gain a slight speed advantage.


you can also call str_replace with arrays of strings as parameters.

$string = str_replace(array("abc","abd","abe") ,
array("%abc","%abd","%abe"), $string);

rush
--
http://www.templatetamer.com/
Jul 17 '05 #3
yawnmoth wrote:
say i was trying to append a % before any occurance of either abc,
abd, or abe, in a string. which would be faster?:

$string = str_replace("abc","%abc",$string);
$string = str_replace("abd","%abd",$string);
$string = str_replace("abe","%abe",$string);

or

$string - preg_replace("/(abc|abd|abe)/","%$1",$string);
A correct preg_replace will be the fastest.
$string = preg_replace("/(ab[cde])/","%$1",$string);

This will be a bit slower than one str_replace, and a lot faster than 3
of them.

also, exactly how would the following to code segments compare, speed
wise?

for ($num=0;$num<$length;$num++) {
$array1[] = $num;
$array2[] = $num;
}

for ($num=0;$num<$length;$num++)
$array1[] = $num;
for ($num=0;$num<$length;$num++)
$array2[] = $num;

it seems like the first one should require 3*$length operations,
whereas the first one would require 4*$length operations, although i'm
not 100% sure on that.


fastest here:
$array1 = $array2 = range(0,$length);

;)

greetings, Christian.
Jul 17 '05 #4
"yawnmoth" <te*******@yahoo.com> wrote in message
news:7j********************************@4ax.com...
say i was trying to append a % before any occurance of either abc,
abd, or abe, in a string. which would be faster?:

$string = str_replace("abc","%abc",$string);
$string = str_replace("abd","%abd",$string);
$string = str_replace("abe","%abe",$string);

or

$string - preg_replace("/(abc|abd|abe)/","%$1",$string);

php.net says that preg_replace is slower than one str_replace, but for
multiple ones, it seems like it would gain a slight speed advantage.
I always like strtr(). Looks neater and doesn't involve the regexp engine.

$table = array(
"abc" => "%abc",
"abd" => "%abd",
"abe" => "%abe"
);
$string = strtr($string, $table);
also, exactly how would the following to code segments compare, speed
wise?

for ($num=0;$num<$length;$num++) {
$array1[] = $num;
$array2[] = $num;
}

for ($num=0;$num<$length;$num++)
$array1[] = $num;
for ($num=0;$num<$length;$num++)
$array2[] = $num;


The correct answer is $array1 = $array2 = range(0, $length - 1);
Jul 17 '05 #5

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

Similar topics

4
by: EP | last post by:
On questions of the suitability of Python for CGI, embedded apps, etc., execution speed comes to mind. I previously read some comparisons which did not show Python in a good light in this regard:...
28
by: Maboroshi | last post by:
Hi I am fairly new to programming but not as such that I am a total beginner From what I understand C and C++ are faster languages than Python. Is this because of Pythons ability to operate on...
3
by: I.V. Aprameya Rao | last post by:
Hi I have to implement a flat file dbms. The basic condition is that relations will be given in files and i will have to run certain select project join queries on those relations. Can...
2
by: aaron | last post by:
Hello, I have two questions, first some background info: We are running SQL 2000 on a Windows 2003 server for out aircraft parts database software. There are 7 workstations (windows 2000...
23
by: Mark Dickinson | last post by:
I have a simple 192-line Python script that begins with the line: dummy0 = 47 The script runs in less than 2.5 seconds. The variable dummy0 is never referenced again, directly or indirectly,...
4
by: Il Prof | last post by:
Hi to everyone! ;) In C language on Linux, what string access method is faster? 1) Puntatori char str="hello"; char* pstr; pstr = str; // access such as "*pstr"
9
by: burningsunorama | last post by:
Hi guys! This is maybe a too 'academic problem', but I would like to hear your opinions, something like pros and cons for each approach.... ... Recently we've had at work a little talk about the...
2
by: ykhamitkar | last post by:
Hi There, I have some questions about ms access database 1. How much data ms access can handle with good speed. 2. Does the size of column affect the speed of ms access database. (If i...
11
by: kyosohma | last post by:
Hi, We use a script here at work that runs whenever someone logs into their machine that logs various bits of information to a database. One of those bits is the CPU's model and speed. While...
7
by: cmrhema | last post by:
Hi, I have two questions. 1. I have heard that replacing a <tr> <td> with <div> tags increases the rendering speed. Is it so, and if yes which speed does it increase, rendering speed or loading...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...

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.