Archive - May 3, 2007

Date

Find the peak memory usage

Profiling, a very interesting topic.
Some time ago, I tried to find the peak memory usage because my program does a lot of memory free actions. A new function added in PHP5 can do just that:
memory_get_peak_usage()
I'm posting this because I know most people are using PHP4 (no offense but... MAKE THE CHANGE! )
I don't know much about command line, which lead to me using some PHP native code to track the highest memory usage.
Remember the ticks? that makes the best profiler. Compare the memory usage now and the previous max memory and chose the larger one over the smaller one.
Note the declare should only run on Linux, on windows this causes apache to crash.
Sample code:

function get_max_mem($dump){
	static $mem;
	$a = memory_get_usage();
	if($a>$mem){
		$mem = $a;
	}
	if($dump){
		return $mem;
	}
}
register_tick_function('get_max_mem');
get_max_mem();
declare(ticks=1);
//now put this in the end of your script
//echo get_max_mem(TRUE);

Unset or set the variable to NULL?

Just out of curiosity, is unset better than setting the variable to null?
since a function call is slower than setting a variable.

I set out to do some experiments so I know what to use later on.

unset($a);
//VS
$a = null;

I did 3 tests, each one of them provide me a conclusion: set a variable to null is faster, but it free a little bit less memory than unset(), unless in a special case I will mention later.
Test 1
$a = range(0,10000);

In this test, set $a to null use only 57% of the time used by unset(). But unset() produces a smaller memory usage(the difference is so small just like the while VS for loop memory difference)

Test 2

$b = range(0,10000);
$a = &$b;

In this test, set $a to null is still faster, and it erased variable $b as well as variable $a.
unset() only erased variable $a, which is most likely what you want.

Test 3

$b = new class_evil;
$a = $b;

The result is same as test one. I did this test because the object is passed by reference.

Each one of these tests are under a while loop that loops it 1000 times, and the result is recorded by using memory_get_usage() and time was recorded by the code from Allan Kent

Notes for May 3

in

Thinking is bad.
How does those Asians who is good at math and suck at sport survives? In the old days they are most likely get eaten by tigers and bears. Natural selection does not make sense. Unless most of my genes are resulted from mutation instead of meiosis.
Then how do I explain 1 billion Chinese who is good at math.
Thinking is bad for me.

I don't think having bad handwriting is weird. Having perfect handwriting is WEIRD. How long does those people practice? write a to z 100 times per day?

I don't think I can do the AP Macro test, it's in the St. John Baptist church, I would catch on fire if I walk in.

Math B regents is making my failing.

The electric outlets are tempting me to stick some metal inside of it.
I could get shock...
But I found that's soo funny...

Honey Pot that kill bots