Coding

PHP Abstract, podcast even I can enjoy

Podcast isn't the kind of media I would like to receive, since reading is much faster than listening.

The above image states the only reason I ever tried the PHP Abstract, Cal Evans is giving out free books to blog about it1! Running Happily
So I picked out 2 podcasts and listened to them...
Oh... cheesy opening...

After hearing the Five Ways To Kill a Software Project. It's mostly about how to not do software project for clients. Some things I definitely want to remember. Not now though. the only client is myself....

The opinionated software one is one thing that's really struck me. I don't mean I'm a person creating millions of configurations for the users, I don't give the users options for configure them because everything I wrote is so delicate2. Sounds like PHP codes for embedded systems. I have the feelings I can never write something suitable for anyone else but me.

The podcast's opening + ending took 1 minute in total, that's quite long compare to the entire podcast. Come on, it's an abstract, extended brief news, cut to the point...

Oh, that reminds me another reason I like reading better. No one can jump though sections in podcast as easy as textbooks.
[Nothing against podcast, I'm just jealous about the people who have a iPod to listening to podcast on the go]

So, I recommend PHP Abstract to anyone who have interest in new(or interesting) stuff in PHP and can find 10 minute on a average week3 sitting in the bus with an ipod.

  1. 1. won't it be cool if I can get something not from my book collection
  2. 2. configuration for prime spirals and matrix rain effect? Don't think so
  3. 3. that's 59.52 milliseconds per average minute

More on Loops--Fusion and Unwinding

I wonder why it's not fusion and defusion...
Loop fusion should be a very common thing in PHP programming.
For example, 2 arrays;

for($i= 0;$i<30;++$i){
	$a[$i]++;
}
for($i = 0;$i<30;++$i){
	$b[$i]++;
}

FUSION!!!
for($i = 0l $i<30; ++$i){
	$a[$i]++;
	$b[$i]++;
}

On the other hand, loop unwinding is rarely seen in PHP.
Unwinding is to reduce the loop overhead by hand code the next few loop into the system.
For example
for($i=0;$i<101;++$i){
echo $i;
}

unwind
for($i=0;$i<101;$i+=4){
echo $i;
echo $i+1;
echo $i+2;
echo $i+3;
}

This technique is proven to have some speed gain, but, unwind is not all powerful, it comes with a cost.
For example, your code will be longer, and harder to read.
and, the unwinding require to have some previous knowledge about the loop. If the loop above is for 100 times instead of 101 times, the 2nd one will output 4 less items.
Even though it is possible to treat this by find the remain ones and use another loop to loop it though, but this make the code more complex than it should.

My entry's source for the PHP contest

Yesterday, the PHP programming test closed, it's time to open source my code!
The entry explained.
See the attachment.

The code and the UI is not user friendly, but it can handle huge dictionaries and it's very memory efficient.
I put an 470kb dictionary in the package for you to test it out.

Hope in the contest, the creators creates super huge dictionaries to test my code out.

Algorithm is my life!
And I know how to improve my algorithm so it's even faster(might be over 60% performance boost)
but, again, it's too complex and I'm too lazy.

Prepend string to a file with stream

I hope you have PHP5.
Yesterday someone in Qunu asked me is there a way to prepend a string in front of a file. I said the only way is to load the entire file into the memory, write the string into the file then append the file in the memory into the file.

I told him to check back my site later, I will come up with something better.
So I did, I would post this yesterday but for some real life reasons, it's postponed till today.
I'm a helpful person, you can Qunu me once in a while if you have PHP problem
This function suppose the script can write to the same directory of the script and you are using PHP5.

function prepend($string, $filename){
	$context = stream_context_create ();
	$fp = fopen($filename,'r',1,$context);
	$tmpname = md5($string);
	file_put_contents($tmpname,$string);
	file_put_contents($tmpname,$fp,FILE_APPEND);
	fclose($fp);
	unlink($filename);
	rename($tmpname,$filename);
}

The function first create a stream, a resource. then convert $fp to a stream. The script puts the string into a temporary file, and append the stream to the end. After that, replace the original file with the temporary file.

Loop multiple associative array at the same time

Today, someone in asked me a question in Qunu asked me a question to loop though 2 associative array at the same time.

Suppose, 2 associative arrays, $a and $b. They have some information corresponding to each other according to their position, but they are not numeric arrays, and it is not possible to manipulate one array's value or key to get another array's key.

My suggestion is to loop though 2 arrays with foreach separately, build them as 2 numeric array and then we will have a numeric key to access them.

foreach($a as $key=>$var){
$num_a[] = array($key=>$var);
}
foreach($b as $key=>$var){
$num_b[] = array($key=>$var);
}

Two foreach loops... if there is more arrays like this, there will be a lot more loops. If the key's information want to be preserved,

The qunu user told me a better code, use each().

foreach($a as $akey=>$avar){
$tmpb = each($b);
$bkey = $tmpb['key'];
$bvar = $tmpb['value'];
}

I didn't have time to ask who he was before he quits from the room.

Honey Pot that kill bots