[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

How do I achieve the same result using sprintf

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
sparingatom
Asked:
sparingatom
  • 4
  • 3
1 Solution
 
Beverley PortlockCommented:
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
 
sparingatomAuthor Commented:
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
 
Beverley PortlockCommented:
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
Industry Leaders: 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!

 
sparingatomAuthor Commented:
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
 
Beverley PortlockCommented:
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
 
sparingatomAuthor Commented:
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
 
Beverley PortlockCommented:
"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

Industry Leaders: 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!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now