dongmi8980 2011-12-15 16:46
浏览 12

使用对最深层次的引用索引到嵌套数组VS?

Given a deeply nested array: $array = array('nested' => array('so' => array('deep' => array())));

...which is faster/'better'?

$array['nested']['so']['deep'][0] = 'a';
$array['nested']['so']['deep'][1] = 'b';
$array['nested']['so']['deep'][2] = 'c';

or

$deep = &$array['nested']['so']['deep'];
$deep[0] = 'a';
$deep[1] = 'b';
$deep[2] = 'c';

Or are they the same? If so, is one preferable, and why?

  • 写回答

2条回答 默认 最新

  • doupeizheng3918 2011-12-15 16:51
    关注

    Keeping a reference is faster and easier to manage, typically. However, it may not make a difference unless you are accessing it multiple times. Hashing is fast. In tests I ran recently I found that hashing is even faster than I had previously supposed. The more levels you bypass by reference, the more improvement you'll see, though.

    However, the most important thing for me is that it is easier to maintain. The performance increase is likely to be negligible in most cases. I wouldn't bother doing it for performance, but if it makes your code more readable, then I'm all for it.

    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致