Hi Robert,

I was going through some php optimization techniques today and found that there is a 3x speed-up if you use:

$flip_var = array_flip($var);
if (isset($flip_var['condition'])) {...}

in place of any in_array('condition', $var) functions.

see: http://blog.maartenballiauw.be/post/2007/01/23/php-code-performance-twea...

I'd like to provide you a patch to change all instances in advcache, but I'd like to know, would you rather have:
a) a patch to patch the patches (mouthful) or
b) have the patches you provide with advcache re-rolled against their respective DRUPAL- versions?

Also, I would prepend any flipped variables with $flip_ and abbreviate the function name if it is used within the in_array(). i.e.

  if (!in_array($node->type, variable_get('advcache_node_exclude_types', array('poll')))) { ... }

becomes:

  $flip_vg_advcache_node_exclude_types = array_flip(variable_get('advcache_node_exclude_types', array('poll')));
  if (!isset($flip_vg_advcache_node_exclude_types[$node->type])) { ... }

unless you'd rather have another naming convention applied.

Please let me know your thoughts.

CommentFileSizeAuthor
#1 time.php_.txt1.85 KBJeremy
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Jeremy’s picture

Status: Active » Closed (works as designed)
FileSize
1.85 KB

This "speedup" is only true if you don't count the time it takes to perform the array_flip(). If you have to do a _very_ large number of lookups on the same unchanging array, you would see a speedup if you performed the array_flip() once, then did a series of calls to isset() on the already flipped array. If you have to flip the array each time, this "speedup" will actually take longer than in_array. I do not think you will find any instances in Drupal where this "speedup" helps.

Please see my attached script to see how I benchmarked this. The script should be run from the command line, and will display output similar to the following:

1 items [no match]: in_array(0.259975910187) flipped1(0.277049064636) flipped2(0.0460340976715)
2 items [no match]: in_array(0.25391793251) flipped1(0.295896053314) flipped2(0.047847032547)
3 items [no match]: in_array(0.259266853333) flipped1(0.304389953613) flipped2(0.0454058647156)
4 items [no match]: in_array(0.274519920349) flipped1(0.323529005051) flipped2(0.0472700595856)
5 items [match]: in_array(0.284055948257) flipped1(0.354227781296) flipped2(0.0491859912872)
6 items [match]: in_array(0.281270027161) flipped1(0.349379062653) flipped2(0.0484509468079)
7 items [match]: in_array(0.282905101776) flipped1(0.396195888519) flipped2(0.04953789711)
8 items [match]: in_array(0.285556077957) flipped1(0.377642154694) flipped2(0.049299955368)
9 items [match]: in_array(0.286665916443) flipped1(0.406566143036) flipped2(0.0499329566956)
[...]
28 items [match]: in_array(0.28941701889) flipped1(0.752466917038) flipped2(0.0473420619965)
29 items [match]: in_array(0.284826993942) flipped1(0.786222934723) flipped2(0.0499889850616)
30 items [match]: in_array(0.282615175247) flipped1(0.764276981354) flipped2(0.0491030216217)

In the above output, in_array() is the time it took to find an item with in_array() 100,000 times, flipped1() is the time it took to flip the array and test isset() 100,000 times, flipped2() is the time it took to flip the array once then test isset() 100,000 times on the flipped array.

The above benchmark was run with PHP 5.2.4.

Marking issue as "by design".