Solved

How do I achieve the same result using sprintf

Posted on 2011-03-08
7
414 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now