在性能上哪个更好:带变量的双引号字符串或带连接的单引号字符串? [英] Which is better on performance: double quoted strings with variables or single quoted strings with concatenations?

查看:113
本文介绍了在性能上哪个更好:带变量的双引号字符串或带连接的单引号字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在PHP中对字符串使用单引号比使用双引号要快,因为PHP不需要检查单引号字符串中是否存在变量.我的问题是哪个会更好:

I know that using single quotes around a string in PHP is faster than using the double quotes because PHP doesn't need to check for variable presence in the single quoted string. My question is which will perform better:

A)带有变量的双引号字符串:

A) A double quoted string with variables present:

echo "foo bar $baz";

B)用连接的变量单引号引起来:

B) Single quoted with a concatenated variable:

echo 'foo bar ' . $baz;

推荐答案

我做了基准在我不久前从事的博客上.但是,当我意识到有很多变量时.其中主要的是:

I did a benchmark of this on a blog I was working on a while ago. However, as I've come to realize there are a lot of variables. Chief among them are:

  1. 您正在执行几个串联?每次执行串联操作时,PHP都会重新解析整个字符串(或者有人告诉我).因此,1个串联可能更快,但是6个串联可能慢得多.
  2. 数据类型.尽管我自己还没有亲自测试过这个,但有人告诉我连接的数据类型也很重要,尽管我不确定多少.

总的来说,我真的不需要担心什么.一般来说,只有在编写大型网站(例如MySpace,Facebook,Flickr等)的情况下,它才会产生明显的变化,并且通常到那时,您背后的硬件已经太多了,单引号和双引号不再相关

Over all I'd say it really isn't that big of a deal for you to actually worry about it. Generally speaking it's only going to make a noticeable difference if you're writing a huge site (think MySpace, Facebook, Flickr, etc) and usually by that point you have so much hardware behind you that the single vs double quotes thing becomes irrelevant again.

我个人会说还有个更重要的事情,它们将以更实质性的方式影响性能(缓存,sql优化,自动加载以防止不必要的包含等).

Personally I'd say there's far more important things that will impact performance in a much more substantial way (caching, sql optimization, auto loading to prevent unnecessary includes, etc).

我几乎每次都亲自选择单引号,但不是为了提高速度.我这样做是因为我认为它更具可读性.这对我来说很重要.

I personally choose single quotes nearly every time, but not for speed. I do because I think it's more readable. And that to me is important.

这篇关于在性能上哪个更好:带变量的双引号字符串或带连接的单引号字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆