Jelajahi Sumber

merged branch kriswallsmith/strpos (PR #3097)

Commits
-------

fe62401 optimized string starts with checks

Discussion
----------

optimized string starts with checks

Doing this with strpos() is slightly faster than substr().

```
Bug fix: no
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -
```

---------------------------------------------------------------------------

by vicb at 2012-01-11T19:58:27Z

How faster ? even if the string is long and do not contain an occurrence of the sub-string ?
Looks like micro-(not)-optimizations to me.

---------------------------------------------------------------------------

by kriswallsmith at 2012-01-11T20:04:26Z

The difference is about 0.1s when repeated 1M times.

---------------------------------------------------------------------------

by vicb at 2012-01-11T20:08:12Z

% would be better (machine & env independant), what string size, what match offset ?
I personally vote against (`substr` is more meaningful to me and I do not like micro-optims)

---------------------------------------------------------------------------

by kriswallsmith at 2012-01-11T20:12:34Z

I personally consider this a coding standard but don't want to bikeshed here :)

---------------------------------------------------------------------------

by vicb at 2012-01-11T20:28:08Z

I have [tried](https://gist.github.com/1596588) at home.
`strpos ` **is** faster unless you have a very long string, probably because you do not need to create a new string, interesting, thanks for the tip.

---------------------------------------------------------------------------

by Tobion at 2012-01-11T22:40:18Z

I think strpos() is more useful. Say you want to change the string you have to replace 2 variables (the text and the length parameter) when using substr(). It could also introduce bugs when they don't match. With strpos() it's only the text.

---------------------------------------------------------------------------

by robocoder at 2012-01-11T22:43:22Z

alternate micro-optimization that doesn't create a temporary string:
```
strncmp($v, "@", 1) === 0
```

---------------------------------------------------------------------------

by Tobion at 2012-01-11T22:47:12Z

@robocoder probably the fastest solution but needs to be benchmarked
Fabien Potencier 13 tahun lalu
induk
melakukan
02a12b2c5c

+ 1 - 1
src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

@@ -79,7 +79,7 @@ class Configuration implements ConfigurationInterface
                     ->useAttributeAsKey('key')
                     ->prototype('array')
                         ->beforeNormalization()
-                            ->ifTrue(function($v){ return is_string($v) && '@' === substr($v, 0, 1); })
+                            ->ifTrue(function($v){ return is_string($v) && 0 === strpos($v, '@'); })
                             ->then(function($v){ return array('id' => substr($v, 1), 'type' => 'service'); })
                         ->end()
                         ->beforeNormalization()

+ 1 - 1
src/Symfony/Component/BrowserKit/Client.php

@@ -441,7 +441,7 @@ abstract class Client
     protected function getAbsoluteUri($uri)
     {
         // already absolute?
-        if ('http' === substr($uri, 0, 4)) {
+        if (0 === strpos($uri, 'http')) {
             return $uri;
         }
 

+ 1 - 1
src/Symfony/Component/Console/Input/ArgvInput.php

@@ -77,7 +77,7 @@ class ArgvInput extends Input
     {
         $this->parsed = $this->tokens;
         while (null !== $token = array_shift($this->parsed)) {
-            if ('--' === substr($token, 0, 2)) {
+            if (0 === strpos($token, '--')) {
                 $this->parseLongOption($token);
             } elseif ('-' === $token[0]) {
                 $this->parseShortOption($token);

+ 1 - 1
src/Symfony/Component/Console/Input/ArrayInput.php

@@ -116,7 +116,7 @@ class ArrayInput extends Input
     protected function parse()
     {
         foreach ($this->parameters as $key => $value) {
-            if ('--' === substr($key, 0, 2)) {
+            if (0 === strpos($key, '--')) {
                 $this->addLongOption(substr($key, 2), $value);
             } elseif ('-' === $key[0]) {
                 $this->addShortOption(substr($key, 1), $value);

+ 1 - 1
src/Symfony/Component/Console/Input/InputOption.php

@@ -46,7 +46,7 @@ class InputOption
      */
     public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
     {
-        if ('--' === substr($name, 0, 2)) {
+        if (0 === strpos($name, '--')) {
             $name = substr($name, 2);
         }
 

+ 1 - 1
src/Symfony/Component/DomCrawler/Link.php

@@ -80,7 +80,7 @@ class Link
         $uri = trim($this->getRawUri());
 
         // absolute URL?
-        if ('http' === substr($uri, 0, 4)) {
+        if (0 === strpos($uri, 'http')) {
             return $uri;
         }
 

+ 1 - 1
src/Symfony/Component/HttpFoundation/Response.php

@@ -134,7 +134,7 @@ class Response
         $charset = $this->charset ?: 'UTF-8';
         if (!$this->headers->has('Content-Type')) {
             $this->headers->set('Content-Type', 'text/html; charset='.$charset);
-        } elseif ('text/' === substr($this->headers->get('Content-Type'), 0, 5) && false === strpos($this->headers->get('Content-Type'), 'charset')) {
+        } elseif (0 === strpos($this->headers->get('Content-Type'), 'text/') && false === strpos($this->headers->get('Content-Type'), 'charset')) {
             // add the charset
             $this->headers->set('Content-Type', $this->headers->get('Content-Type').'; charset='.$charset);
         }

+ 1 - 1
src/Symfony/Component/HttpFoundation/ServerBag.php

@@ -23,7 +23,7 @@ class ServerBag extends ParameterBag
     {
         $headers = array();
         foreach ($this->parameters as $key => $value) {
-            if ('HTTP_' === substr($key, 0, 5)) {
+            if (0 === strpos($key, 'HTTP_')) {
                 $headers[substr($key, 5)] = $value;
             }
             // CONTENT_* are not prefixed with HTTP_

+ 1 - 1
src/Symfony/Component/HttpKernel/Kernel.php

@@ -597,7 +597,7 @@ abstract class Kernel implements KernelInterface
     {
         $parameters = array();
         foreach ($_SERVER as $key => $value) {
-            if ('SYMFONY__' === substr($key, 0, 9)) {
+            if (0 === strpos($key, 'SYMFONY__')) {
                 $parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
             }
         }

+ 1 - 1
src/Symfony/Component/HttpKernel/Profiler/MysqlProfilerStorage.php

@@ -24,7 +24,7 @@ class MysqlProfilerStorage extends PdoProfilerStorage
     protected function initDb()
     {
         if (null === $this->db) {
-            if ('mysql' !== substr($this->dsn, 0, 5)) {
+            if (0 !== strpos($this->dsn, 'mysql')) {
                 throw new \RuntimeException('Please check your configuration. You are trying to use Mysql with a wrong dsn. "'.$this->dsn.'"');
             }
 

+ 1 - 1
src/Symfony/Component/HttpKernel/Profiler/SqliteProfilerStorage.php

@@ -25,7 +25,7 @@ class SqliteProfilerStorage extends PdoProfilerStorage
     protected function initDb()
     {
         if (null === $this->db || $this->db instanceof \SQLite3) {
-            if ('sqlite' !== substr($this->dsn, 0, 6 )) {
+            if (0 !== strpos($this->dsn, 'sqlite')) {
                 throw new \RuntimeException('You are trying to use Sqlite with a wrong dsn. "'.$this->dsn.'"');
             }
             if (class_exists('SQLite3')) {

+ 1 - 1
src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php

@@ -431,7 +431,7 @@ class StubIntlDateFormatter
         $timeZone = $timeZoneId;
 
         // Get an Etc/GMT time zone that is accepted for \DateTimeZone
-        if ('GMT' !== $timeZoneId && 'GMT' === substr($timeZoneId, 0, 3)) {
+        if ('GMT' !== $timeZoneId && 0 === strpos($timeZoneId, 'GMT')) {
             try {
                 $timeZoneId = DateFormat\TimeZoneTransformer::getEtcTimeZoneId($timeZoneId);
             } catch (\InvalidArgumentException $e) {

+ 3 - 3
src/Symfony/Component/Yaml/Parser.php

@@ -114,7 +114,7 @@ class Parser
                 }
 
                 if ('<<' === $key) {
-                    if (isset($values['value']) && '*' === substr($values['value'], 0, 1)) {
+                    if (isset($values['value']) && 0 === strpos($values['value'], '*')) {
                         $isInPlace = substr($values['value'], 1);
                         if (!array_key_exists($isInPlace, $this->refs)) {
                             throw new ParseException(sprintf('Reference "%s" does not exist.', $isInPlace), $this->getRealCurrentLineNb() + 1, $this->currentLine);
@@ -188,7 +188,7 @@ class Parser
 
                     if (is_array($value)) {
                         $first = reset($value);
-                        if (is_string($first) && '*' === substr($first, 0, 1)) {
+                        if (is_string($first) && 0 === strpos($first, '*')) {
                             $data = array();
                             foreach ($value as $alias) {
                                 $data[] = $this->refs[substr($alias, 1)];
@@ -347,7 +347,7 @@ class Parser
      */
     private function parseValue($value)
     {
-        if ('*' === substr($value, 0, 1)) {
+        if (0 === strpos($value, '*')) {
             if (false !== $pos = strpos($value, '#')) {
                 $value = substr($value, 1, $pos - 2);
             } else {