measuring difference between single and double quotes

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

in later versions of the phpBB 2.0.x branch they switched most, if not all strings to single quote that I remember.
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post by dbevfat »

Imagine a test:

Code: Select all

$db->connect();
$rows = $db->fetchAll();
foreach ($rows as $row)
{
  // do something with columns from $row and quotes
}
For 1M loops the difference between single and double quotes was about 0,2s -- as measured by other users in this thread, of course it depends on the amount of quote-juggling-code in the test. So, we could expect that for 10M loops the difference would be 2 seconds. The whole time would be over 100 seconds, but that doesn't matter, because both tests would take that much. The user would wait around 100s or 2 seconds more. He would never notice.

To sum up: something takes 102 seconds, and you can "improve" it to take "only" 100 seconds by replacing double quotes with single. Big optimization. Why not do something to get rid of those 100 seconds?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Are you kidding? I wish it took only 100 seconds. I'm not even going to do this test with 10M rows.
As you said "Imagine a test" :lol: no way I'm doing this test.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Any such iterative test creates an aggregate execution time - sure that ups the measured time but still does nothing about individual runs where string quoting is likely negligible compared to bigger fish. Might as well connect to a database 10M times and blame it...;)

Didn't know phpBB switched - I wonder what gains they experienced?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

There are two different ways to quote strings in PHP - either with single quotes or with double quotes. The main difference is that the parser does variable interpolation in double-quoted strings, but not in single quoted strings. Because of this, you should always use single quotes unless you specifically need variable interpolation to be done on that string. This way, we can save the parser the trouble of parsing a bunch of strings where no interpolation needs to be done.
Source: http://area51.phpbb.com/docs/coding-guidelines.html

I've found this page long time ago... They don't say anything related to the performance and speed but it's quite obvious that when PHP doesn't need to do any variable interpolation the code will run faster.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

The pudding is in the measurement of the gain...:)
Post Reply