?
Solved

How do I achieve the same result using sprintf

Posted on 2011-03-08
7
Medium Priority
?
422 Views
Last Modified: 2012-05-11
I am using str_replace to search through a string and strip out a word. But it sems to be slowing down my script.

$result = str_replace('word', '', $example);

Open in new window

I've hear sprint is faster. So how can I acieve the same result using sprintf?

Thanks!
0
Comment
Question by:sparingatom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35073520
str_replace is not that slow. I suspect you have other problems. Why not post the script here and show us what's happening.
0
 

Author Comment

by:sparingatom
ID: 35073592
The script is fine as it was working well. I added that line of code and the script is noticeably slower.  (the script is processing 1,500+ varaibles) so even if its a little bit slower it makes a big difference.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35073724
Yes, but it depends WHERE you texted that piece of code. str_replace is not slow. Have you placed inside a loop?

Try using a regex if you want to see a real slowdown.

sprintf can replace known values in a formatted string, but str_replace finds values in unknown locations and replaces them. The two are not really equivalent.
0
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 

Author Comment

by:sparingatom
ID: 35073916
Yes, its inside of a foreach loop.  Not sure what you mean by known values.

I know where in the string the word is i want to srip out its at the end.


The actual code im using looks like this:
	//Strip out titles already containing the word large. 
		$result = str_replace(' large', '', $row['prodname']);

		$entry = array(
			'title' => html_escape($result),
			'link' => html_escape($link),
			'description' => html_escape($desc),
			'g:product_type' => "$row['prodcat"],
			'g:expiration_date' => $expirationDate,
			'g:id' => $row['productid'],
			'g:condition' => html_escape(strtolower($row['prodcondition'])),
			'g:price' => $price
		);

Open in new window

0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 1500 total points
ID: 35074237
What I meant was that with sprintf you take a string like this

"Here is my string and I want my replacement here %s"

So the replacement text is easily found because it has a known marker (the %s) but you are taking a string where the 'markers' (the words to be replaced) need to be located in the string first so sprintf and str_replace are actually very similar and so is their performance.

"I know where in the string the word is i want to srip out its at the end."

Normally for something like this I would recommend a regex, but they are typically 5 to 7 times SLOWER than str_replace.....
0
 

Author Closing Comment

by:sparingatom
ID: 35074845
Not sure why he would want to suggest somthing slower than what I'n already using when I am going to speed.  I appriciate and answer to my question but I didn't like that he suggested somthing is wrong with my code before he answerd the question.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35081404
"Not sure why he would want to suggest somthing slower than what I'n already using when I am going to speed."

I was not suggesting you use something slower, I was saying that what I would normally recommend is slower than str_replace. You will notice that I did not offer any regex patterns to accomplish the task because it was clear that the performance would be unacceptable.


"I didn't like that he suggested somthing is wrong with my code before he answerd the question"

We rarely see the whole picture when we answer a question and many people leave out details which they consider unnecessary but which may be vital in resolving the issue. In memory operations such as str_replace are the fastest  bit of the PHP/MySQL software so when someone says that using a very, very fast routine is slowing their code it doesn't feel right. Normally in this environment the biggest slowdowns are bad (or no) indexes in MySQL or loops in PHP. That's why I said what I said. Perhaps I should have worded it better.

Look at this PHP page http://uk2.php.net/microtime which deals with measuring script execution times. Adapt this and put it in your problem script and measure the time that various stages of the script take. You can even explicitly measure the time taken by str_replace. I think that the results would be very helpful to you in tracking down where the time is being used up.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question