Emmanuel Benoist Home Page

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
Furthermore, a timer may be started and stopped many times and the elapsed time of the timer is the sum of all partial durations.
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 function rand($min,$max) that generates a number between $min and $max

Solution

Run the test:testSortTimer.php