Cast to int is the fastest way to sanitize integer input

PHP plays fast and loose with its data types which is one of the features that folks coming from more structured languages find very off-putting.

In phpland it's also the fastest way to sanitize an input which you expect to be an integer.

<?php $var = (int) $var;?>

The result is an integer. If the value of value of var is a number, it's converted into an integer. If it's not a number or doesn't begin with a number, then it's given a value of 0.

Here's a quick test script to illustrate the point. It tests both success and failure conditions.

And the results of this test when run with php 5.5?

php ./test.php 
Time to complete 1000000 (int) conversion filters:0.46194696426392
Time to complete 1000000 int_val filters:0.72814893722534
Time to complete 1000000 preg_replace filters:6.5657861232758
Time to complete 1000000 filter_var(x,FILTER_SANITIZE_NUMBER_INT) filters:1.5239408016205

Done

The test does a million repetitions of numeric values and not-numeric values. The result being type cast is almost twice as fast as intval(), 3.7 times faster than filter_var() and 18x faster than preg_replace(). If we had not done this in a tight loop the results wouldn't have been so dramatic but you get the idea.

 That's something to keep in the toolbox the next time you encounter an integer that needs to be filtered.

Laggy Nexus 7 after KitKat Update? Do a factory reset.

Oh KitKat, I really wanted to like you. You took my perfectly functional Nexus7 and its silky smooth menus and app switching and wrecked it. Now I have a whole lot of hurry up and wait. Wait. WAIT!

downloadI haven't been this frustrated with an operating system since Windows 95.
Well, it appears the the solution is really simple: Wipe and Reinstall.

2012_12_10_17.48.54

If you've been backing up your device into the cloud, google will automatically reinstall all your apps, contacts etc when you login. My guess is my device was really full when KitKat installed and really needed more space than I had.

If you're a nexus 7 user about to throw away your tablet, give this a try first.

Comparison of Latency

I’ve seen this somewhere before and I couldn’t locate it so I reproduced it.

Disk is really slow compared other storage options.
This is why the #1 optimization for web applications is in-memory caching.