Exercise: Base Syntax of PHP II
A timer class
The goal of this exercise is to design and implement a class which would provide some basic timer functionalities, allowing to measure the time needed for some operations during the elaboration of a dynamic page for example.This timer class should at least provide the following functions:
- A constructor method to initialise the timer
- A
start-method to start the timer. This method should verify that one cannot start an already started timer - A
stop-method to stop the timer and store the elapsed time. A timer can only be stopped if it is running. - A
getTimer-method to get the elapsed time measured by the timer - A
lap-method, which returns the elapsed time without stopping the timer. - A
reset-method to clear the values stored by the timer and sets the attributes in their default values
In order to write a solution, you need to use the following expression:
$m = microtime(TRUE);this returns a number representing the current time in seconds with the precision up to the microsecond.
Measure Efficiency of Bubble Sort and Quick Sort
Write a test programm, that generates a list containing numbers. Measure the duration of the bubble sort and quick sort algorithms in this case, and measure the duration of the php sort algorithm. Do this experiment with lists of 100, 1000, 10000 numbers. Does it corresponds to the value comportement expected regarding the Algorithmic course? You will use the functionrand($min,$max) that generates a number between $min and $max
Solution
Run the test:testSortTimer.php- The test program
- timer.php the file containing the class
Timer - bubbleSort.php the file containing the bubble sort function.
- quickSort.php the file containing the quick sort class.
Related Pages
Contact
Prof. Dr. Emmanuel Benoist
Berner Fachhochschule - TI
Quellgasse 21
CH-2501 Biel/Bienne
Switzerland
Mail: emmanuel.benoist (at) bfh.ch
Berner Fachhochschule - TI
Quellgasse 21
CH-2501 Biel/Bienne
Switzerland
Mail: emmanuel.benoist (at) bfh.ch
Social Networks
Follow
me
on
Linkedin, Scholar
& Research gate