2

In order to reduce the amount of data being retrieved from my server by Ajax requests, I'm planning on converting about 30 boolean fields into one binary number, and then using bitwise operators to make use of this data.

I haven't used bitwise operators before and was wondering if they're fast/slow in PHP and JavaScript, i.e. how does building a binary number and then comparing it with another using a bitwise operator perform compared to just looking up a boolean value stored in an array/object?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wheresrhys
  • 22,558
  • 19
  • 94
  • 162
  • 8
    I'm willing to bet a considerable sum that any performance problems in your app will NOT be caused by a bitwise operation. – Jamiec Dec 13 '10 at 10:38
  • possible duplicate of [Performance of bitwise operators in javascript](http://stackoverflow.com/questions/1523061/performance-of-bitwise-operators-in-javascript) – Felix Kling Dec 13 '10 at 10:40
  • *(related)* [Bitwise Operations in PHP](http://stackoverflow.com/questions/2131758/bitwise-operations-in-php/2131828#2131828) – Gordon Dec 13 '10 at 11:22

4 Answers4

3

Bitwise operations in general are the fastest operations in higher level languages. In case of PHP, they map to the underlying C, so they don't cause significant performance dropping by any means.

mike3996
  • 17,047
  • 9
  • 64
  • 80
  • 1
    Correction: They are the fastest operations in ALL languages. At the lowest level of hardware they are the only operations ;-) – phkahler Dec 13 '10 at 11:54
  • 2
    But in javascript all numbers are stored in floating point, so the number must be casted to an integer, bitwise operated on, and casted back to floating point, causing a bit of a performance hit. – Chad Schouggins Jul 29 '14 at 16:20
2

According to the documentation from Mozilla, bitwise operations are very slow in JavaScript since internally the numbers are treated as floating point values, not int64. So I think you should avoid using this in your JavaScript whenever possible.

In PHP, I don't know and did not see any formal documentation about this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PCMan
  • 21
  • 1
1

The only way you can do it is by measuring - although I doubt doing bitwise operations in JavaScript will get you much over an array/object.

I'd try:

var len = 100000;
var bitwise = 0xdeadbeef;
var arrays = [1,1,0,1,1,1,1,0,1,0,1,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1];
var start = new Date().getTime();
for (var i = 0; i < len; i++) {
   arrays[i % 32]; //Uncomment to test this
   //bitwise & (1 << (i % 32)); //Uncomment to test this
}
var end = new Date().getTime();
console.log((end-start)/len +"ms per access");

I got (Chrome, Windows 7, Core i7 920, 6 GB RAM):

0.0015 ms per access for arrays
0.0016 ms per access for bit shifting.

You make the decision - it is almost insignificant. Use Arrays for slightly better speed, use bitwise packing if space (bandwidth) is to be conserved.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chii
  • 14,540
  • 3
  • 37
  • 44
  • Thanks for putting the effort in to testing it. Interesting that arrays is slightly faster, which probably means there will be a measurable difference in my app (as I will have to calculate the integer from teh array each time I want to test too) – wheresrhys Dec 13 '10 at 12:13
1

Sure there are big performance costs - namely those of your own performance.

First, changing bool fields to bits won't optimize anything. It won't make your application any faster. Second, you said you haven't used bitwise operations before, so you'll lose a lot of time learning new stuff which is hardly useful in web programming. Third, your code gets order of magnitude less readable, which means major support and refactoring costs in the nearest future. Bottom line: don't waste your time.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user187291
  • 53,363
  • 19
  • 95
  • 127
  • I see your point (particularly with regard to code readability), but it WILL make the app faster/more efficient from the point of view of there being much less data flying around (I will save at least a few kB per AJAX request I reckon.. and there are A LOT of AJAX requests). And while I haven't used bitwise operators before the reason is probably the same as for many programmers: I haven't had the need to, not because they're difficult to learn. The behaviour of the basic operators (& in particular) looks pretty straightforward to me. – wheresrhys Dec 13 '10 at 12:09
  • 1
    The app only uses one request per user action (repopulating a google map in response to a drag/zoom), and though I have gzip enabled, I hope a significant proportion of users will be accessing it on mobiles, so I have to be mindful of slow connections. – wheresrhys Dec 13 '10 at 15:04
  • 4
    -1 This is one of the most toxic answer in the last ... days or so. What do you mean by wasting your time learning? Bitwise ops are not used in web programming? Well, I use it ALL THE TIME! Because is easier to put this SQL condition "WHERE type & 6 != 0" rather than "WHERE type == 2 OR type == 4" ... There have fun http://www.postgresql.org/docs/9.3/static/datatype-binary.html ... Edit: http://stackoverflow.com/questions/1277470/looking-for-real-world-examples-of-bitwise-operations-in-php – StefanNch Nov 30 '13 at 09:51