News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

PHP String Replacement Speed Comparison

Started by Thantos, June 03, 2007, 03:00:23 PM

Previous topic - Next topic

Joshua Dickerson

sprintf() isn't really in the same boat as the other string replacement functions IMO. It is more of a fancy concatenation and in that boat, it isn't so fast. If you have a string that looks like your sprintf() string and you want to return or output it, use a language construct (double quotes, dots, commas, etc) instead.
Come work with me at Promenade Group



Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

Thantos

There is of course one major problem with doing that:  It requires you to determine the language grammer in the source and not in the language strings.

Lets take the following statement:
My name is {NAME} and I am a {JOB} here at {COMPANY} where I work for {BOSSTITLE} {BOSS}
example:  My name is Thantos and I am a developer here at Simple Machines where I work for Lead Developer Compuart

Now if I wanted to that with concatation (of any sort) it would be something like: ($t is an array of language strings)
$t[1] . $name . $t[2] . $job . $t[3] . $company . $t[4] . $bosstitle . ' ' . $boss
So if I wanted to that sentence I'd need to use four text strings.  But now lets say for my particular setting "Lead Developer Compuart" isn't proper, I need "Compuart the Lead Developer".  Not only have I inserted another text string ("the") but I have rearranged the order in which the data appears.  No amount of changing of (just text strings) $t[1] . $name . $t[2] . $job . $t[3] . $company . $t[4] . $bosstitle . ' ' . $boss
is going to get me My name is Thantos and I am a developer here at Simple Machines where I work for Compuart the Lead Developer

Now if I put it all into one text string and use some some sequence identifiers I can do this:
My name is %1$s and I am a %2$s here at %3$s where I work for %4$s %5$s
and call it like: $str = sprintf($text, $name, $job, $company, $bosstitle, $boss);
Now if they want to make the change I did then it is a simple edit job to the language string
My name is %1$s and I am a %2$s here at %3$s where I work for %5$s the %4$s

Heck you could even leave the boss title out and have
My name is %1$s and I am a %2$s here at %3$s where I work for %5$s

One thing to consider is if you want to use
My name is %1$s and I am a %2$s here at %3$s where I work for %4$s %5$s
and gain the speed or use
My name is {NAME} and I am a {JOB} here at {COMPANY} where I work for {BOSSTITLE} {BOSS}
and gain the readability.

That choice depends mostly on how the string is being used.

Joshua Dickerson

What's wrong with the following:
$str  = "My name is $NAME and I am a $JOB here at $COMPANY where I work for $BOSSTITLE $BOSS";
Come work with me at Promenade Group



Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

Thantos

Can you export that into a language file so it can be translated into other languages?

What happens if you load the file that contains the string and those variables aren't defined yet?

Joshua Dickerson

Quote from: groundup on June 12, 2007, 04:15:16 PM
$txt['no_sprintf']  = "My name is $GLOBALS[NAME] and I am a $GLOBALS[JOB] here at $GLOBALS[COMPANY] where I work for $GLOBALS[BOSSTITLE] $GLOBALS[BOSS]";
or something similar to that. As for not being defined, okay, I guess there is where you have the difference. Although, %1$s can hardly be considered readable.
Come work with me at Promenade Group



Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

SleePy

Quote from: groundup on June 12, 2007, 04:35:57 PM
Quote from: groundup on June 12, 2007, 04:15:16 PM
$txt['no_sprintf']  = "My name is $GLOBALS[NAME] and I am a $GLOBALS[JOB] here at $GLOBALS[COMPANY] where I work for $GLOBALS[BOSSTITLE] $GLOBALS[BOSS]";
or something similar to that. As for not being defined, okay, I guess there is where you have the difference. Although, %1$s can hardly be considered readable.

Still isn't very readable. But I choose speed over readability of the language strings myself ;) I don't care if I can't read them if it makes my forum twice as fast to use it another way I will.
Jeremy D ~ Site Team / SMF Developer ~ GitHub Profile ~ Join us on IRC @ Libera.chat/#smf ~ Support the SMF Support team!

Joshua Dickerson

If you want speed, language constructs work better. Although, the speed difference between all of these is very little.
Come work with me at Promenade Group



Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

Daniel15

Quotesprintf() isn't really in the same boat as the other string replacement functions IMO. It is more of a fancy concatenation and in that boat, it isn't so fast.
It's not classified as "fancy concatenation"; it's classified as string formatting ;). Still, I agree in a sense - It is a formatting function, not a replacement function :)
And I agree with Thantos' large post above :)
Daniel15, former Customisation team member, resigned due to lack of time. I still love everyone here :D.
Go to smfshop.com for SMFshop support, do NOT email or PM me!

Advertisement: