Search
Jason Rahm - Jason's Blog
You are here: DevCentral > Weblogs

posted on Wednesday, May 20, 2009 3:20 PM

I'd like to think that the community was on vacation last week, or that the winning entry, which happened to be the first and only entry, was so brilliant that it scared everyone off.  A week ago Friday, I kicked off what I hope will be a frequent experience, opening up a simple optimization contest to the community in the forums.  The challenge was to take the dice rolling iRule from Lori's post on multi-classing your load balancer, optimize it, and submit it.  One community user, natty76, took the challenge.  So, by default, natty76, you are the Optimize This! inaugural champion, congratulations!

Natty76's Winning Entry

when RULE_INIT { 
  set static::max 10 
  set static::f(0) { $num } 
  set i 1 
  while { $i < $static::max } { 
    eval "set static::f($i) { $static::f([expr $i - 1]) + int(rand() * \$d) }" 
    incr i 
  } 
} 
when HTTP_REQUEST { 
  if { [HTTP::uri] starts_with "/roll"} { 
    set num [URI::query [HTTP::uri] "num"] 
    set d [URI::query [HTTP::uri] "d"]
    HTTP::respond 200 content "$num d $d = [expr $static::f($num)]" 
  } 
}

 

Natty76's use of RULE_INIT to build the expression eliminates the need for the per-request for loop in Lori's rule, and results in a savings of 13.55% in clock cycles.  Nice work, Nat.  This could be pruned a little more for maximum efficiency, but there's something to be said for readability.  Natty76 also noted a couple potential downsides to this implementation.  One, there is no error handling as the code assumes that $num and $d are always within the limit of the array.  Two, the memory footprint required to store the array are higher as $num & $d are higher.

Thanks much, Natty76, hopefully you'll have more (OK, some) competition next go-round.

 

 

Follow me on Twitter Follow me on LinkedIn Follow me on Facebook Add to Technorati Favorites

 



Feedback

No comments posted yet.

Let Me Know What You Think


Please use the form below if you have any comments, questions, or suggestions.

Title:
 
Name:
 
Email: (so we can show your gravatar)
Website:
Comment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
 
Please add 8 and 2 and type the answer here:

Blog Stats

Posts:86
Comments:108
Stories:0
Trackbacks:0
  

82,243 Members in 102 Countries and Growing!

Join DevCentral Today!

About DevCentral

DevCentral has been a successful, thriving community for many years. We have always strived to bring you the best technical documentation, discussion forums, blogs, media and much more that we can.

So dive in, get familiar with DevCentral. We hope you like it, we hope it makes your job easier, and lets you get that much more power out of the community. To learn more, make sure to check out the Getting Started section. And if you have any problems, or think something could be easier to use, drop us a line to let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback       or