note 120160 added to language.types.string

From: Date: Mon, 14 Nov 2016 15:05:36 +0000
Subject: note 120160 added to language.types.string
Groups: php.notes 
Request: Send a blank email to php-notes+get-207099@lists.php.net to get a copy of this message
I’ve been a PHP programmer for a decade, and I’ve always been using the ‘single-quoted literal’ and ‘period-concatenation’ method of string creation. But I wanted to answer the performance question once and for all, using sufficient numbers of iterations and a modern PHP version. For my test, I used: php -v PHP 7.0.12 (cli) (built: Oct 14 2016 09:56:59) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies ------ Results: ------- * 100 million iterations: $outstr = 'literal' . $n . $data . $int . $data . $float . $n; 63608ms (34.7% slower) $outstr = "literal$n$data$int$data$float$n"; 47218ms (fastest) $outstr =<<<EOS literal$n$data$int$data$float$n EOS; 47992ms (1.64% slower) $outstr = sprintf('literal%s%s%d%s%f%s', $n, $data, $int, $data, $float, $n); 76629ms (62.3% slower) $outstr = sprintf('literal%s%5$s%2$d%3$s%4$f%s', $n, $int, $data, $float, $data, $n); 96260ms (103.9% slower) * 10 million iterations (test adapted to see which of the two fastest methods were faster at adding a newline; either the PHP_EOL literal, or the \n string expansion): $outstr = 'literal' . $n . $data . $int . $data . $float . $n; 6228ms (reference for single-quoted without newline) $outstr = "literal$n$data$int$data$float$n"; 4653ms (reference for double-quoted without newline) $outstr = 'literal' . $n . $data . $int . $data . $float . $n . PHP_EOL; 6630ms (35.3% slower than double-quoted with \n newline) $outstr = "literal$n$data$int$data$float$n\n"; 4899ms (fastest at newlines) * 100 million iterations (a test intended to see which one of the two ${var} and {$var} double-quote styles is faster): $outstr = 'literal' . $n . $data . $int . $data . $float . $n; 67048ms (38.2% slower) $outstr = "literal$n$data$int$data$float$n"; 49058ms (1.15% slower) $outstr = "literal{$n}{$data}{$int}{$data}{$float}{$n}" 49221ms (1.49% slower) $outstr = "literal${n}${data}${int}${data}${float}${n}" 48500ms (fastest; the differences may seem small but this held true across multiple runs of the test, and this was always the fastest variable encapsulation style) * 1 BILLION iterations (testing a completely literal string with nothing to parse in it): $outstr = 'literal string testing'; 23852ms (fastest) $outstr = "literal string testing"; 24222ms (1.55% slower) It blows my mind. The double-quoted strings "which look so $slow since they have to parse everything for \n backslashes and $dollar signs to do variable expansion", turned out to be the FASTEST string concatenation method in PHP - PERIOD! Single-quotes are only faster if your string is completely literal (with nothing to parse in it and nothing to concatenate), but the margin is very tiny and doesn’t matter. So the "highest code performance" style rules are: 1. Always use double-quoted strings for concatenation. 2. Put your variables in "This is a ${variable} notation", because that is the FASTEST way of ALL. Do NOT write it in the slower “This is a $variable notation" or "This is a {$variable} notation". 3. Feel free to use single-quoted strings for TOTALLY literal strings such as array keys/values, variable values, etc, since they are a TINY bit faster when you want literal non-parsed strings. But I had to do 1 billion iterations to find a 1.55% measurable difference. So the only real reason I'd consider using single-quoted strings for my literals is for code cleanliness, to make it super clear that the string is literal. 4. If you really think another method such as sprintf() or 'this'.$var.'style' is more readable, and you don’t care about maximizing performance, then feel free to use whatever concatenation method you prefer. ---- Server IP: 72.52.91.14 Probable Submitter: 79.136.69.250 ---- Manual Page -- http://php.net/manual/en/language.types.string.php Edit -- https://master.php.net/note/edit/120160 Del: integrated -- https://master.php.net/note/delete/120160/integrated Del: useless -- https://master.php.net/note/delete/120160/useless Del: bad code -- https://master.php.net/note/delete/120160/bad+code Del: spam -- https://master.php.net/note/delete/120160/spam Del: non-english -- https://master.php.net/note/delete/120160/non-english Del: in docs -- https://master.php.net/note/delete/120160/in+docs Del: other reasons-- https://master.php.net/note/delete/120160 Reject -- https://master.php.net/note/reject/120160 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#207099) next »