Log in

View Full Version : Retrieval or Processing which is faster



letom
12-23-2013, 02:49 PM
i have two choices, i need to know which is faster


(1)Retrieving the percentage result from database which is already been stored.

OR

(2)Adding 10000 different numbers(which includes only numbers 1,2,3) = Total Value

Multiplying these 10000 x 100 and dividing it to Total Value gives the percentage result ..


which utilizes the server resources more ?if it is second option why ?

traq
12-23-2013, 04:07 PM
Depends on your server. For example, finding the average of 10,000 numbers between 1 - 3 isn't really lot of work. Likewise, querying a DB is going to take a varying amount of time depending on where it is - could be nearly instantaneous if it's on the same machine, or could take over a second if it's on a remote server. Why don't you test it?

letom
12-23-2013, 05:18 PM
Thanks..
what are the different methods which i can test it on my local machine. ?

traq
12-24-2013, 04:20 AM
The simplest method to benchmark (time) something, is to put it inside a function:
function myBenchmark(){
/* bunches of code */
}

Then, take note of the time, run the function a few thousand times, then note the time again:
$start = microtime( true );

for( $i=0; $i<1000; $i++ ){
myBenchmark();
}

$elapsed_time = microtime( true ) - $start;
print $elapsed_time;

Then, do the same thing with the other method and compare the results.

Some things to note:

1) MAKE SURE everything happens the same way each time. If your code does something that affects the outside environment, for example (maybe you check for a variable and do some calculation if it doesn't exist - but then, on the next iteration, it does exist), then that would skew your results.

2) 1,000 iterations is just an example; it's probably too few. Adjust the count until it takes at least a few seconds to finish. (Bigger numbers, smaller deviation.)

3) Repeat this process a dozen or so times and take an average.

This won't necessarily tell you which will work faster in real life, however. There are lots of considerations. It'll give you a starting point, though.