PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Fri Aug 28, 2009 11:54 pm
For the fun of it, here's the benchmarks I have. Times below are averages over 20K iterations including
peak memory usage for all iterations[1].
Code: Select all
astions : 0.00032996s 7027 KB
astions2 : 0.00011592s 7027 KB
mikemike : 0.00006905s 6047 KB
tasairis : 0.00006718s 6048 KB
tasairis_loopless1 : 0.00008440s 6038 KB[2]
tasairis_loopless2 : 0.00056466s 6074 KB[2]
johncartwright : 0.00010650s 6222 KB[3]
pytrin_loopless : 0.00009284s 7019 KB
The numbers are a bit different because I rearranged the benchmarking code. Should only measure a function call and the actual work now.
[1] Memory usage is proportional to the number of iterations so it seems there are memory leaks somewhere. Use this figure for comparisons between methods only.
[2] Includes simple modifications so that create_function is not called multiple times.
[3] Doesn't do the back-and-forth thing.
Last edited by
requinix on Sat Aug 29, 2009 2:34 am, edited 1 time in total.
Eran
DevNet Master
Posts: 3549 Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME
Post
by Eran » Sat Aug 29, 2009 1:59 am
what about john's solution? how does it perform?
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sat Aug 29, 2009 2:32 am
pytrin wrote: what about john's solution? how does it perform?
I thought it missed the snakes-and-ladders-style requirement...
Code: Select all
echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", $e);'), array_chunk(array_reverse(range(1, 100)), 10)));
Code: Select all
100 99 98 97 96 95 94 93 92 91
90 89 88 87 86 85 84 83 82 81
80 79 78 77 76 75 74 73 72 71
70 69 68 67 66 65 64 63 62 61
60 59 58 57 56 55 54 53 52 51
50 49 48 47 46 45 44 43 42 41
40 39 38 37 36 35 34 33 32 31
30 29 28 27 26 25 24 23 22 21
20 19 18 17 16 15 14 13 12 11
10 9 8 7 6 5 4 3 2 1
Regardless,
Code: Select all
johncartwright : 0.00010650s 6222 KB
mikemike
Forum Contributor
Posts: 355 Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK
Post
by mikemike » Sat Aug 29, 2009 8:50 am
Good job guys
I'll come up with a new (harder) one soon.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Sat Aug 29, 2009 9:38 am
LOL That's great!
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Sat Aug 29, 2009 10:09 am
John Cartwright wrote:
Code: Select all
echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", $e);'), array_chunk(array_reverse(range(1, 100)), 10)));
It doesn't meet the challenge criteria though
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Aug 29, 2009 1:42 pm
Thats what I get for thinking I do not need to test
Here is my updated version, and the more I think about it, you've actually got it wrong. The Starting point is actually on the left.
If you want the "correct version",
Code: Select all
echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", substr($e[0], 0, 1) % 2 ? array_reverse($e) : $e); '), array_chunk(array_reverse(range(1, 100)), 10)));
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Sat Aug 29, 2009 2:43 pm
Yet another solution:
Code: Select all
<?php
header('Content-Type: text/plain');
for ($i = 100; $i > 0; $i -= 20) {
echo implode(' ', range($i, $i - 9)), PHP_EOL
, implode(' ', range($i - 19, $i - 10)), PHP_EOL;
}
?>
And another:
Code: Select all
for ($i = 100; $i > 0; $i -= 20) {
for ($j = $i; $j > $i - 10; $j--) {
echo $j, ' ';
}
echo PHP_EOL;
for ($k = $i - 19; $k < $i - 9; $k++) {
echo $k, ' ';
}
echo PHP_EOL;
}
I'm not sure how we're calculating "7 lines".
Edit: This post was recovered from search engine cache.
Last edited by
McInfo on Wed Jun 16, 2010 7:41 pm, edited 1 time in total.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sat Aug 29, 2009 3:42 pm
John Cartwright wrote: Code: Select all
echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", substr($e[0], 0, -1) % 2 ? array_reverse($e) : $e); '), array_chunk(array_reverse(range(1, 100)), 10)));
So close... It's that 91-100 line:
Code: Select all
91 92 93 94 95 96 97 98 99 100
81 82 83 84 85 86 87 88 89 90
If you change the substr to use 0,-1 instead of 0,1 it's perfect.
Code: Select all
with loops
astions1 : 0.00032996s 7027 KB
astions2 : 0.00011592s 7027 KB
mikemike : 0.00006905s 6047 KB
tasairis : 0.00006718s 6048 KB
mcinfo1 : 0.00006479s 6247 KB
mcinfo2 : 0.00005899s 6450 KB
without loops
tasairis1 : 0.00008440s 6038 KB[2]
tasairis2 : 0.00056466s 6074 KB[2]
pytrin : 0.00009284s 7019 KB
johncartwright : 0.00014869s 6230 KB[2]
[2] Includes simple modifications so that create_function is not called multiple times.
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Tue Sep 01, 2009 2:47 am
A recursive, loopless one
Code: Select all
function gen($start, $end, $reverse = 0)
{
if ($start > 0)
return (
implode(" ",
range(
$start - $reverse*20 + $reverse,
$end - !$reverse*20 + 20 + !$reverse
)
).
"\r\n".
gen($start - $reverse*20, $end - !$reverse*20, !$reverse)
);
}
echo gen(100, 90);
=>
Code: Select all
function gen($start, $end, $reverse = 0)
{
if ($start > 0)
return (implode(" ", range($start - $reverse*20 + $reverse, $end - !$reverse*20 + 20 + !$reverse))."\r\n".gen($start - $reverse*20, $end - !$reverse*20, !$reverse));
}
echo gen(100, 90);
PS: Sorry for the delay, I was on vacation
Last edited by
VladSun on Tue Sep 01, 2009 2:50 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Sep 01, 2009 2:49 am
Ah decomposition in action
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Tue Sep 01, 2009 3:00 am
mikemike wrote: Print list of numbers from 1-100. These numbers should be in rows of 10 and in a zig-zag fashion.
First I thought you meant the Z-scan used in JPEG format -
http://en.wikipedia.org/wiki/File:JPEG_ZigZag.svg
Maybe it could be a good challenge too
There are 10 types of people in this world, those who understand binary and those who don't
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Tue Sep 01, 2009 3:57 am
astions wrote: I was trying to be cool by decreasing the number of lines which compromised performance.
I think that's a valid goal, but I also think solutions for optimised performance, and for optimised readability are valid too. These Challenges should be about thinking up new and interesting approaches to problems, regardless of
how they're interesting.
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Tue Sep 01, 2009 4:09 am
Just for completeness, here's my solution. It should be pretty quick considering it's not doing very much.
Code: Select all
<style>
#board {
float: left;
width: 600px;
height: 600px;
border: 10px solid #888;
}
.square {
width: 60px;
height: 60px;
color: #888;
}
.l {
float: left;
}
.r {
float: right;
}
</style>
<div id="board">
<?php
for ($x=100;$x>0;$x--) {
if ($x%10==0) {
$dir = ($c++%2==0) ? "l" : "r";
}
echo "<div class=\"square ".$dir."\">".$x."</div>";
}
?>
</div>
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Sep 01, 2009 5:28 am
Very nice onion2k, I didn't think to create it using css.
Might want to put an @ sign in front of the $c