Advertisement:

Author Topic: PHP String Replacement Speed Comparison  (Read 130714 times)

Offline Joshua Dickerson

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 12,777
  • Gender: Male
    • joshuaadickerson on GitHub
    • joshuaadickerson on LinkedIn
Re: PHP String Replacement Speed Comparison
« Reply #20 on: June 12, 2007, 09:24:36 AM »
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.
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?

Offline Thantos

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 16,163
  • Gender: Male
Re: PHP String Replacement Speed Comparison
« Reply #21 on: June 12, 2007, 10:39:42 AM »
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:
Code: [Select]
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)
Code: [Select]
$t[1] . $name . $t[2] . $job . $t[3] . $company . $t[4] . $bosstitle . ' ' . $bossSo 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)
Code: [Select]
$t[1] . $name . $t[2] . $job . $t[3] . $company . $t[4] . $bosstitle . ' ' . $bossis going to get me
Code: [Select]
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:
Code: [Select]
My name is %1$s and I am a %2$s here at %3$s where I work for %4$s %5$sand 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
Code: [Select]
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
Code: [Select]
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
Code: [Select]
My name is %1$s and I am a %2$s here at %3$s where I work for %4$s %5$sand gain the speed or use
Code: [Select]
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.

Offline Joshua Dickerson

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 12,777
  • Gender: Male
    • joshuaadickerson on GitHub
    • joshuaadickerson on LinkedIn
Re: PHP String Replacement Speed Comparison
« Reply #22 on: June 12, 2007, 04:15:16 PM »
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";
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?

Offline Thantos

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 16,163
  • Gender: Male
Re: PHP String Replacement Speed Comparison
« Reply #23 on: June 12, 2007, 04:28:24 PM »
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?

Offline Joshua Dickerson

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 12,777
  • Gender: Male
    • joshuaadickerson on GitHub
    • joshuaadickerson on LinkedIn
Re: PHP String Replacement Speed Comparison
« Reply #24 on: June 12, 2007, 04:35:57 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.
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?

Offline SleePy

  • Site Team Lead
  • SMF Master
  • *
  • Posts: 30,341
  • Gender: Male
  • Thats his happy face.
    • jdarwood007 on GitHub
    • @jdarwood on Twitter
    • SleePy Code - My personal site
Re: PHP String Replacement Speed Comparison
« Reply #25 on: June 12, 2007, 04:49:47 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
Support the SMF Support team!
Profiles:
GitHub

Offline Joshua Dickerson

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 12,777
  • Gender: Male
    • joshuaadickerson on GitHub
    • joshuaadickerson on LinkedIn
Re: PHP String Replacement Speed Comparison
« Reply #26 on: June 12, 2007, 05:33:36 PM »
If you want speed, language constructs work better. Although, the speed difference between all of these is very little.
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?

Offline Daniel15

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 4,222
  • Gender: Male
  • http://dan.cx/
    • daaniel on Facebook
    • Daniel15 on GitHub
    • daniel15 on LinkedIn
    • @Daniel15 on Twitter
    • Daniel15
Re: PHP String Replacement Speed Comparison
« Reply #27 on: June 13, 2007, 01:07:06 AM »
Quote
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.
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!