HBH PHP Competition
The former PHP Optimization contest was unfortunately canceled due to some issues, but I've decided that a new one shall be made!
So, this weekend I will release a script written mainly by jjbutler88 that is to be optimized.
I also figured that I could release the prizes and rules while we wait so that you all can look forward to the release date.
Rules:
- The script can be edited as much as you'd like, as long as the output is the same.
- If two identical scripts are turned in, the first delivered will be the one participating.
- To participate in the contest, you will have to deliver the script in a PM to me, not by forum or any other method.
- Everyone can participate (even administrators and myself).
- The winner will be the person with the least execution time on the script.
- Prizes and rules may change at any point.
Prizes: 1st - 100 points + 1 month EM. 2nd - 75 points. 3rd - 50 points.
End date: 23 May 2009
If you have any questions or suggestions, please don't hesitate with sending me a PM or replying to this thread.
The script!
<?php
//
// Script written by jjbutler88 for the HBH PHP optimization competition, organised by -cL
// Probably the worst script I have written in my entire life. Coding angst supressed by listening to 'Clair de Lune' on repeat :)
//
echo "The following script is to be optimised to run the fastest by running the code inbetween the comments.<br/>";
echo "The file 'words.txt' must be opened and read. Then the words contained in the file must be seperated into the following catagories (words can fall into more than 1 catagory):<br/><br/>";
echo "+ Letters only<br/>";
echo "+ Contains letters and numbers<br/>";
echo "+ Contains punctuation<br/>";
echo "<br/>The arrays must contain no duplicates, and must be sorted alphanumerically where possible.";
echo "<br/>Output begins below:<br/>";
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$startTime = explode(" ", microtime());
///////////////////////////////////
// DO NOT EDIT ABOVE THIS LINE! //
//////////////////////////////////
$file = file_get_contents('words.txt');
$lettersOnly = array();
$lettersAndNumbers = array();
$punctuation = array();
$letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$numbers = array('1','2','3','4','5','6','7','8','9');
$punctuationChars = array('.','?','!','-','(',')');
$words = explode(' ', $file);
$y = sizeof($words);
for($x = 0; $x < $y; $x++)
{
foreach($numbers as $num)
{
if(strpos($words[$x], $num) !== FALSE)
{
$lettersAndNumbers[] = $words[$x];
}
}
foreach($punctuationChars as $p)
{
if(strpos($words[$x], $p) !== FALSE)
{
$punctuation[] = $words[$x];
}
}
if(!in_array($words[$x], $lettersAndNumbers) && !in_array($words[$x], $punctuation))
{
$lettersOnly[] = $words[$x];
}
}
$lettersOnly = array_unique($lettersOnly);
$lettersAndNumbers = array_unique($lettersAndNumbers);
$punctuation = array_unique($punctuation);
sort($lettersOnly);
sort($lettersAndNumbers);
sort($punctuation);
echo "+ Letters only: ";
print_r($lettersOnly);
echo "<br/><br/>+ Contains letters and numbers: ";
print_r($lettersAndNumbers);
echo "<br/><br/>+ Contains punctuation: ";
print_r($punctuation);
///////////////////////////////////
// DO NOT EDIT BELOW THIS LINE! //
//////////////////////////////////
$mem = memory_get_peak_usage();
$endTime = explode(" ", microtime());
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$executionTime = floatval($endTime[0] - $startTime[0]);
echo "Execution time: $executionTime ms<br/>";
echo "Peak memory allocation: ".$mem."<br/>";
$score = round($mem * $executionTime, 3);
echo "Score: $score - Lower is better!";
?>
words.txt
Hackers
PHP
Optimization
Competition
Written
By
Jjbutler88
And
-cL
In
The
Worst
PHP
Possible
(Much
Harder
Than
It
Might
Seem)
1337ness
Is
In
The
Eye
Of
The
Beholder
Jah
Rastafari```
>
Will the scripts be tested several times? Because, as stated above, there are some ups and downs when executing a script.
Yes.
>
Second question: In the original text file, are there spaces before / after each word, or not.
There are spaces.
-cL wrote: [quote]system_meltdown wrote: Where do we submit to?
PM Me ^__^
-cL wrote:
- To participate in the contest, you will have to deliver the script in a PM to me, not by forum or any other me [/quote]
Wouldn't that mean you can't participate? Because obviously you could steal other member's scripts if they perform better than your own.
@spyware - the maths isnt perfect, I knocked it up in a free afternoon.
@everyone else - For example of how your input should look, check here:
http://athleet.netii.net/hbh.php
It seems settled that I will collect the scripts, so PM me your entries when you finish them and they will be tested on/after the end date for the competition.
-cL wrote:
- The script can be edited as much as you'd like, as long as the output is the same. That's pretty vague. If I know what the output's supposed to be, I could just tell it to print that and not do any work.
Also, can we alter the text file?
EDIT: also, is it the fastest time that wins, or the best score (time and memory)?
EDIT AGAIN: Why is the final output included in the timer? If that has to be the same for everyone, it should be at the end with the rest of the results.
jjbutler88 wrote: @spyware - the maths isnt perfect, I knocked it up in a free afternoon.
@everyone else - For example of how your input should look, check here:
http://athleet.netii.net/hbh.php
It seems settled that I will collect the scripts, so PM me your entries when you finish them and they will be tested on/after the end date for the competition.
Will they be tested on your localhost? Or on a shared webhost? Because remember, shared hosting will be a hell of a lot slower.
MoshBat wrote: [quote]system_meltdown wrote: [quote]jjbutler88 wrote: @spyware - the maths isnt perfect, I knocked it up in a free afternoon.
@everyone else - For example of how your input should look, check here:
http://athleet.netii.net/hbh.php
It seems settled that I will collect the scripts, so PM me your entries when you finish them and they will be tested on/after the end date for the competition.
Will they be tested on your localhost? Or on a shared webhost? Because remember, shared hosting will be a hell of a lot slower.[/quote]
I wonder… Why can't we test them here?[/quote]
Because that requires uploading each script, running, then deleting from HBH's server. It's just easier if the guy collecting the scripts just runs them from localhost.
MoshBat wrote: [quote]system_meltdown wrote: [quote]MoshBat wrote: [quote]system_meltdown wrote: [quote]jjbutler88 wrote: @spyware - the maths isnt perfect, I knocked it up in a free afternoon.
@everyone else - For example of how your input should look, check here:
http://athleet.netii.net/hbh.php
It seems settled that I will collect the scripts, so PM me your entries when you finish them and they will be tested on/after the end date for the competition.
Will they be tested on your localhost? Or on a shared webhost? Because remember, shared hosting will be a hell of a lot slower.[/quote]
I wonder… Why can't we test them here?[/quote]
Because that requires uploading each script, running, then deleting from HBH's server. It's just easier if the guy collecting the scripts just runs them from localhost.[/quote]
Quite true, but that could rule out the whole running programs affecting speed issue… Although, a few tests and a mean seems appropriate…[/quote]
Have it run on a box with nothing else running other than Apache? So like, no torrenting applications, no MSN, etc.
You need to be more specific about the rules for this. Are we supposed to assume we don't know the contents of the file? Because if we do, we could just code the lists (already sorted) straight into the php. And if we're going to go that far, we might as well just echo what the output is supposed to be.
ynori7 wrote: You need to be more specific about the rules for this. Are we supposed to assume we don't know the contents of the file? Because if we do, we could just code the lists (already sorted) straight into the php. And if we're going to go that far, we might as well just echo what the output is supposed to be.
I think jjbutler88 would need to specify that seeing as he wrote the script.
The script says at the top, you need to read the text file containing the words and sort them accordingly.
Please just use common sense here, there are no trick questions and secret workarounds, everybody just use your php optimization skills to try and obtain a consistently low score, then PM the scripts to -cL. Simples.
jjbutler88 wrote: Please just use common sense here, there are no trick questions and secret workarounds, everybody just use your php optimization skills to try and obtain a consistently low score, then PM the scripts to -cL. Simples. Right, that's as I figured. There's no confusion from me, but I've talked to a number of other people who weren't so clear. Unless you make the rules very clear and specific, you will end up with a lot of invalid entries to sort through.
IMPORTANT SCRIPT CHANGE!!!
Instead of this
///////////////////////////////////
// DO NOT EDIT BELOW THIS LINE! //
//////////////////////////////////
$endTime = explode(" ", microtime());
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$executionTime = floatval($endTime[0] - $startTime[0]);
echo "Execution time: $executionTime ms<br/>";
echo "Peak memory allocation: ".memory_get_peak_usage()."<br/>";
$score = round(memory_get_peak_usage() * $executionTime, 3);
echo "Score: $score - Lower is better!";
The script now is changed to
///////////////////////////////////
// DO NOT EDIT BELOW THIS LINE! //
//////////////////////////////////
$mem = memory_get_peak_usage();
$endTime = explode(" ", microtime());
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$executionTime = floatval($endTime[0] - $startTime[0]);
echo "Execution time: $executionTime ms<br/>";
echo "Peak memory allocation: ".$mem."<br/>";
$score = round($mem * $executionTime, 3);
echo "Score: $score - Lower is better!";
So the script saves the memory_get_peak_usage(); in a variable instead of using it twice.
Thanks to system_meltdown for correcting this!
-cL wrote: IMPORTANT SCRIPT CHANGE!!!
Instead of this
///////////////////////////////////
// DO NOT EDIT BELOW THIS LINE! //
//////////////////////////////////
$endTime = explode(" ", microtime());
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$executionTime = floatval($endTime[0] - $startTime[0]);
echo "Execution time: $executionTime ms<br/>";
echo "Peak memory allocation: ".memory_get_peak_usage()."<br/>";
$score = round(memory_get_peak_usage() * $executionTime, 3);
echo "Score: $score - Lower is better!";
The script now is changed to
///////////////////////////////////
// DO NOT EDIT BELOW THIS LINE! //
//////////////////////////////////
$mem = memory_get_peak_usage();
$endTime = explode(" ", microtime());
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$executionTime = floatval($endTime[0] - $startTime[0]);
echo "Execution time: $executionTime ms<br/>";
echo "Peak memory allocation: ".$mem."<br/>";
$score = round($mem * $executionTime, 3);
echo "Score: $score - Lower is better!";
So the script saves the memory_get_peak_usage(); in a variable instead of using it twice.
Thanks to system_meltdown for correcting this!
shouldn't
///////////////////////////////////
// DO NOT EDIT BELOW THIS LINE! //
//////////////////////////////////
$mem = memory_get_peak_usage();
$endTime = explode(" ", microtime());
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$executionTime = floatval($endTime[0] - $startTime[0]);
echo "Execution time: $executionTime ms<br/>";
echo "Peak memory allocation: ".$mem."<br/>";
$score = round($mem * $executionTime, 3);
echo "Score: $score - Lower is better!";
be:
///////////////////////////////////
// DO NOT EDIT BELOW THIS LINE! //
//////////////////////////////////
$endTime = explode(" ", microtime());
$mem = memory_get_peak_usage();
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$executionTime = floatval($endTime[0] - $startTime[0]);
echo "Execution time: $executionTime ms<br/>";
echo "Peak memory allocation: ".$mem."<br/>";
$score = round($mem * $executionTime, 3);
echo "Score: $score - Lower is better!";
So that you don't include setting the mem variable in the time of script execution. Not that it is a huge difference… worth pointing out though.
p4plus2 wrote: [quote]-cL wrote: IMPORTANT SCRIPT CHANGE!!!
Instead of this
///////////////////////////////////
// DO NOT EDIT BELOW THIS LINE! //
//////////////////////////////////
$endTime = explode(" ", microtime());
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$executionTime = floatval($endTime[0] - $startTime[0]);
echo "Execution time: $executionTime ms<br/>";
echo "Peak memory allocation: ".memory_get_peak_usage()."<br/>";
$score = round(memory_get_peak_usage() * $executionTime, 3);
echo "Score: $score - Lower is better!";
The script now is changed to
///////////////////////////////////
// DO NOT EDIT BELOW THIS LINE! //
//////////////////////////////////
$mem = memory_get_peak_usage();
$endTime = explode(" ", microtime());
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$executionTime = floatval($endTime[0] - $startTime[0]);
echo "Execution time: $executionTime ms<br/>";
echo "Peak memory allocation: ".$mem."<br/>";
$score = round($mem * $executionTime, 3);
echo "Score: $score - Lower is better!";
So the script saves the memory_get_peak_usage(); in a variable instead of using it twice.
Thanks to system_meltdown for correcting this!
shouldn't
///////////////////////////////////
// DO NOT EDIT BELOW THIS LINE! //
//////////////////////////////////
$mem = memory_get_peak_usage();
$endTime = explode(" ", microtime());
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$executionTime = floatval($endTime[0] - $startTime[0]);
echo "Execution time: $executionTime ms<br/>";
echo "Peak memory allocation: ".$mem."<br/>";
$score = round($mem * $executionTime, 3);
echo "Score: $score - Lower is better!";
be:
///////////////////////////////////
// DO NOT EDIT BELOW THIS LINE! //
//////////////////////////////////
$endTime = explode(" ", microtime());
$mem = memory_get_peak_usage();
echo "<br/>-----------------------------------------------------------------------------------------<br/>";
$executionTime = floatval($endTime[0] - $startTime[0]);
echo "Execution time: $executionTime ms<br/>";
echo "Peak memory allocation: ".$mem."<br/>";
$score = round($mem * $executionTime, 3);
echo "Score: $score - Lower is better!";
So that you don't include setting the mem variable in the time of script execution. Not that it is a huge difference… worth pointing out though.[/quote]
It doesn't really matter either way, because then you could be escalating the memory allocation by getting the end time. Or slightly increasing the time by having another variable set.
Hmm, nobody who wants to brag about the incredibly low execution time (s)he has achieved yet? That surprises me. :)
By the way, I find it very difficult to determine which is the fastest method of the things I've tried so far. My execution time keeps jumping around in quite a big range each time I try. Perhaps I should try this on something else than my crappy WinXP machine with WAMP installed and LOTS of other processes running. ;)
I do like this challenge though. B)
GTADarkDude wrote: Hmm, nobody who wants to brag about the incredibly low execution time (s)he has achieved yet? That surprises me. :)
By the way, I find it very difficult to determine which is the fastest method of the things I've tried so far. My execution time keeps jumping around in quite a big range each time I try. Perhaps I should try this on something else than my crappy WinXP machine with WAMP installed and LOTS of other processes running. ;)
I do like this challenge though. B)
Because what's low for someone else could be high for another person, so there's no point trying to "brag", because no one knows what the "lowest" score can be.
The script is too small and too quick in execution time to make optimisation noticable. I get some +/- 10%-20% variation in points from the same script. And I doubt the best performance you can get is better then 20% faster. So it's not even possible to test and see if there is a real difference between the one we code and in the original one. From optimisation, I'm making, im getting and an estimated boost of performance of around 10%, but again it's hard for me to really know if its really 10% since the original script sometimes run at same speed as mine.
Edit: Do the "do not edit" part is part of the challenge ?
Thanaton wrote: Will the scripts be tested several times? Because, as stated above, there are some ups and downs when executing a script.
Second question: In the original text file, are there spaces before / after each word, or not. Because if there aren't, the original script doesn't work :O
Greatings, Thanaton
These questions have been answered already!
-
-cL made some changes to the execution time measurement code, I don't think it will make much difference but hey, that's what you got to work with
-
I know the original script doesn't work without spaces, its a bad script. Use the text file as it was, that's not part of the optimization challenge.
For crying out loud, why cant people just use the grey matter a bit and optimize the code instead of asking pointless questions??
jjbutler88 wrote: For crying out loud, why cant people just use the grey matter a bit and optimize the code instead of asking pointless questions?? Because they aren't pointless questions. This competition was not well organized, and after hearing these simple questions, you or -cL should have gone back to the main description and updated it with clearer details, but neither of you did. Since none of these questions have been answered in a clear way, you shouldn't be surprised that they are being repeated.
I have a question about the testing. If the difference between 2 score is of less then 5%, how can you really tell a script was faster since of PHP fluctuation ?
Considering most optimisation are gonna have around the same speed, how can you tell if someone script was faster or slower if the difference is too low ?
sToRm_seveN wrote: Why the hell does this script have HTML in it. A CLI script would fair much better.
It was written to output as a web page, not assuming any CGI functionality in the web server. From what I gather, -cL will be testing this on a local XAMPP, so CGI will not necessarily be available/configured.
MoshBat wrote: Look, guys. Just take the script and make it faster. It is that simple. Obviously, but logistics are still important. For all we know, -cL is just going to draw names from a hat to decide the winner, in which case there's no point in participating.
Obviously things haven't been though through fully, so these questions serve the purpose of kicking the guy in charge into gear so he can get his act together.
I'm removing all this stupid HTML from my entry. I hope you don't mind.
EDIT: And I'm wondering how you're going to reiterate this script. I hope you're able to figure it out so you don't affect the memory usage.
EDIT 2: Does the output have to be 100% identical? For instance, mine is an actual list instead of that print_r() crap.
sToRm_seveN wrote: I'm removing all this stupid HTML from my entry. I hope you don't mind.
Well I'm pretty sure they will mind, due to the fact there's HTML at the top, right by the part that says DO NOT EDIT.
sToRm_seveN wrote: EDIT 2: Does the output have to be 100% identical? For instance, mine is an actual list instead of that print_r() crap.
I'm assuming that as long as the script does what it's meant to and writes out what it needs to, it doesn't matter HOW you make it write it out.
Do we have to code the script now, as If we don't know the word list?
Because if not, we could just check the words for numbers and punctuation and if there are none of them, the words are right as "lettersonly" and we don't have to check for letters with this wordlist. But that would not really work in a real case, because there could be other special chars and then a word with a "@", "{" etc in it would be "lettersonly".