Tip of the Moment
In Windows XP/Vista/7 open the 'Start' menu and in the box at the bottom type 'MSCONFIG' and hit [ENTER]. On the fourth tab from the left, across the top titled 'Startup' open that and then select the programs that you need to start with your computer every time it starts (this will not be everything!) then un-check the programs that you don't need to start every time. Click 'Apply' and then 'Okay' and you my friend have saved some (often considerable) time off your startup! FYI make sure you DO NOT prevent your anti-virus and firewall from starting up :) |
| A Brief Lesson in Coding Efficiency |
|
|
|
| Software Development - PHP Programming |
| Written by Todd Nestor |
| Sunday, 03 July 2011 23:06 |
|
Alright, so today we are going to compare two sets of code that do the exact same thing: generate prime numbers! Exciting, right? I use this example because my boss at one of the first software development companies I worked for gave me a challenge, to create a prime number generator in seven lines of code or less. I did so, and now its down to one line (kind of), but is it really the most efficient code? Not at all, but it does its job. I also created another version of the code that runs much more efficiently, but it is significantly longer. Let's begin. I want to start by showing you the one-line code and explaining it. Here's that code: This one generates all prime numbers between 0 and 10000.
Here's a little cleaner version of it to make it easier to read:
Here is the code explained: The first line: <?does the same thing as <?phpit just starts the php code section.\ The second line: for($i=2; $i<10000; $i++)starts a for loop setting the variable $i to the initial value of 2 and continuing to loop while $i is less than 10000 and increasing the variable $i by one each time it is run. The third line: { is simply the opening bracket containing everything that will happen during a single instance of our for loop.
The fourth line: for($x=2; $i%$x!=0; $x++)is the line that does most of the magic. It initiates a for loop to check if our number is prime. It starts the variable $x at the value 2 and loops until $i divided by $x has 0 remainder. $x increases by one each iteration. The % operator returns the remainder from a division operation. ex. 10%3 would return 1. The fifth line: { is simply the opening bracket for everything to happen during each iteration of that for loop.
The sixth line: echo $x == ($i-1) ? $i." ":"";is a shorthand if statement saying that if $x is equal to $i-1 then our number must be prime, so write it. If its not, write nothing. The seventh line: }is simply the closing bracket for everything that happens during the second for loop. The eighth line: }is simply the closing bracket for everything that happens during our initial for loop. The ninth line: ?>closes the php sent to the server for compiling. Now let's look at the Efficient version:
This one works a little differently: Obviously the first line opens up the php code, the second line sets a variable $primes = array(2);which simply makes $primes an array with the 2 as the first value. We will use this array to store all of our prime numbers. The next line is for($i=3; $i<10000; $i++)which starts a for loop setting the variable $i to the initial value of 3 and continuing to loop while $i is less than 10000 and increasing the variable $i by one each time it is run. The $i variable is the number we are testing on each iteration, since we know 2 is prime and already added it to the array we don't need to test it. Next we have foreach ($primes as $prime)this line initiates a loop that is run on each value in the $primes array. For each loop $prime is the value of which we are currently on, so as we add more items to the $primes array this loop will take longer. Next we have if ($i % $prime == 0)which tests the number we are currently on $i to see if it can be divided evenly by the current prime number $prime we are on. If it can then this number is not prime so the next line break; stops the loop and moves on to the next integer $i to test if its prime. The next line elseif ($prime >= sqrt($i))tests to see if we have made it to the square root of $i yet. If we have and this line of code is being run then our number is prime, so the next line $primes[] = $i; sets it as the next value in our $primes array. The reason we only test up to the square root is to save time, because every number larger than the square root that divides evenly into our $i will result in a number smaller than the square root if divided into it. For example: the square root of 100 is 10, so numbers larger than 10 that divide into 100 (20, 25, 50) produce numbers smaller than the square root as answers (5, 4, 2 respectively). So we would have found those answers in all the numbers we checked up to 10. So by only checking through 10 you save all sorts of time. Now the reason we are only testing prime numbers is also to save time, because every number factors down into prime numbers, for example let's again take 100: factors are 2, 4, 5, 10, 20, 25, and 50. 2 and 5 are already prime, 4 gets broken back up into 2 and 2 which are prime, 10 gets broken up into 2 and 5 which are prime, 20 to 10 (already covered) 2, 4 (already covered) and 5, 25 to 5 and 5 which are prime 50 down into 2, 5, 10 (already covered), 25 (already covered) So everything eventually gets broken down into 2 and/or 5 in this case, and I assure you every number that is not prime gets broken down into prime numbers at its root factors, so there is no need to test any numbers that aren't prime. This program would test 2, 3, 5, and 7 on 100 (those are all the prime numbers less than its square root: 10). So we didn't have to check 4, 6, 8, 9 or 10. On larger numbers the savings is even greater. Below is a chart showing how much faster the efficient version is than the one line version for 0 to different values: As you can see the efficient one is faster from the start, but if the one line one was faster for the first part it'd probably be worth it to set the code to use the oneline for 0 - wherever the separation point is and then switch to the efficient version after that. Alright, thats all I've got for you, until next time! |
| Last Updated on Tuesday, 05 July 2011 04:40 |



