Intarray

PHP arrays are overkill for storing primitive integer values. They are slow and consume huge amounts of memory. Did I mention that serialization of PHP arrays adds a lot of overhead?

Intarray (Integer array) PHP extension is a space and time efficient tool for handling large int32_t arrays and performing lookups and set operations. Basically each intarray is just a PHP binary string and Intarray extension provides a bunch of functions for performing operations on it.

Some real world use cases with user ids:

  • Is user online? (binary search to an array of online users)
  • Gimme common friends of me and some other user (intersection of two intarrays)
  • Store members of a group to cache in an efficient manner
  • And so on..
Usage

Intarray has two modes of operation:

  1. Array - Values in random order. May contain duplicates.
  2. Set - Unique values in ascending order.

The latter mode enables use of binary search and set operations that utilize merge algorithm. It is up to the user to ensure that appropriate functions are used on each type of array.

Examples<?php // Binary search // Create an empty array full of zeroes $a = intarray_create(100); // Fill it with some values in ascending order for ($n = 0; $n < 100; $n++) intarray_set($a, $n, $n * 4); // Perform an O(log n) search and find index of value 188 $index = intarray_binarysearch($a, 188); echo $index;?>

... and we got 47

<?php // Union // Create two intarrays $a = intarray_create_from_array(array(1, 2, 3)); $b = intarray_create_from_array(array(3, 4, 5)); // Get a union of them $u = intarray_union($a, $b); // Dump to screen intarray_dump($u);?>

... and we got { 1, 2, 3, 4, 5 }

Caveats
  • Data is not stored in network byte order (endianess). You must be cautious if different CPU architectures are involved.
  • Ordered data is not guarded against illegal modifications. Maybe next major version has an OO-style wrappers for the array.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值