Page 100 - Open Soource Technologies 304.indd
P. 100
Web Technologies-I
Notes
Functions calls are expensive. Let’s eliminate them. readInt, readShort, fseek are now inlined.
Recursion changed to iteration (e.g. 14 000 less function calls). Able to process 400 queries per
second compared to the previous 210.
We see that the latest profiling results have twice the number of freads and unpacks than fseeks.
It seems that fseek is used to seek out the right position, read two numbers with unpacking
them. The implementation confirms that luckily we could just read once (2 bytes more) and
unpack once (2 unpackings with one invocation).
$a =unpack(‘N’, fread($this->m_file, 4));
$np[‘ip’] = $a[1];
$a =unpack(‘n’, fread($this->m_file, 2));
$np[‘key’] = $a[1];
// this can be changed to
$np =unpack(‘Nip/nkey’, fread($this->m_file, 6));
How does this version stack up to the Java version? Let’s disable profiling and run 100 000
iterations. Vanilla version processes ~850 IPs, when functions are inlined the number is around
1400. Java version can still do 6000.
Let’s try caching. Peeking at the Java implementation shows that Java caching version
(whopping 141 242 IPs per second – yup 141k) uses just a byte[] array and makes lookups
from there instead of seeking and reading from file. Easy, let’s do the same in PHP.
We read everything into a string and instead of fread with access the string elements with the
offset. For fseek with just set the offset. We are using 600kb more memory but can increase
the throughput to ~2800.
As it seems I have just wasted a night, I just should have checked the Computer Language
Benchmarks. PHP in the sense of execution speed is uncomparable to Java.
Questions:
1. Point out the shortcomings of PHP.
2. Explain the functions that are used in the case study.
Self Assessment
Multiple choice questions:
4. Which one is not a variable scope?
(a) Local (b) Global
(c) External (d) Static
5. ................ parameters enable you to specify a default value for function parameters.
(a) Variable (b) Default
(c) Missing (d) Value
Fill in the blanks:
6. Local, globes and ............... are the three types of variables used in PHP functions.
7. ................ functions would not work with Language constructs.
94 LOVELY PROFESSIONAL UNIVERSITY