July 11, 2008
Optimizing Perlin Noise

Via todays post on nodename about Perlin Clouds and Frocessing I came across Ron Valstar’s AS3 version of Ken Perlin's Improved Noise reference implementation which I must have missed last year.

As Ron mentions the AS3 code runs way slower than the native perlinNoise() method that Flash's BitmapData class offers, but on the other hand his class is true to the original version and offers real 3D Perlin Noise and the possibility to control the falloff factor. So the reason for my belated post now is that I couldn't help noticing several opportunities to optimize the code. As usual optimizing will definitely not make code look better or easier to understand but for stuff like PerlinNoise which you usually called several hundred or thousand times every little counts to make it perform faster.

I attacked the class at the usual optimization points:

- Unroll functions wherever possible - this is usually the biggest timesaver. almost every function call in an inner loop is poison for execution time. The reason for that is that internally every call causes tons of save-data-on-stack and get-data-from-stack operations which do not happen if you copy the code that the function processes right into your loop.

- Do type casting whenever dealing with arrays in Flash. Especially for the indices - so instead of v = p[i+5] say v = Number( p[int(i+5)] ). That doesn't look as nice but it helps

You can see the result of this optimization here On the left is the original class, on the right the optimized one which turns out to be twice as fast. I had hoped for more but that's all I could do.

You can download OptimizedPerlin.as here or right-click the example and choose "View source".

Posted at July 11, 2008 07:04 PM | Further reading

Yay Mario!

Posted by: Alan Shaw on July 11, 2008 07:28 PM

Awesome! Thanks!

Posted by: Brett Widmann on July 11, 2008 07:50 PM

Oh btw - in case anybody wonders why I left those three Math.floor in there: it turned out that the typical optimizations x>>0 or int(x) not always give the same result as Math.floor(x). int(x) works only for positive numbers and x>>0 is only correct for x < 0x80000000.

Posted by: Mario Klingemann on July 11, 2008 10:16 PM

hi mario
great work!

btw. i'm not that expert of optimizations but have you tried to replace the for loops with while loops especially in the fill loop?

i tried it in a bitmap processing project and the performance boost was really amazing.

Posted by: alex on July 12, 2008 02:10 AM

congrats to the author and thanks for sharing ;) (as always)

first comment on your blog, we met in Paris.

concerning the loops, yesterday a friend of mine noticed something strange regarding the loops.
I did a benchmark and it appears that the type of the max iteration is far more important than it seems(to me at least). while is not necessarily faster btw.
please have a look: http://en.nicoptere.net/?p=7

Mario > you (unwillingly?) pushed me to write in english (when we met in Paris, we took a taxi) my blog is for you ;)

Posted by: nicoptere on July 12, 2008 03:35 PM

Hey Mario! Great work! I'll have to replace the code in my older perlin experiments (http://sebleedelisle.com/?s=perlin) and see what happens...



Posted by: Seb Lee-Delisle on July 12, 2008 04:40 PM

Few suggestions here :

- don't use statics ! instead construct an instance and it should be a lot faster

- if you want to it be even faster, use haXe ;)

Posted by: Nicolas on July 20, 2008 08:20 PM

Just gave it a try : watch http://blog.haxe.org/entry/35


Posted by: Nicolas on July 20, 2008 09:48 PM

Great job Nicolas! About the statics: that was the first thing I changed in my optimizations but to my own surprise this didn't improve the performance at all. But maybe that was because I wanted to keep it a Singleton like the original class.

Posted by: Mario Klingemann on July 21, 2008 12:59 PM

Wow, that's quite some speed increase :-) great job !
Gonna give it a go it right away.
And I'll try to remember the speed optimizations next time I write some processor hungry stuff.

Posted by: Ron on July 27, 2008 11:07 AM

I'm a newbie to Flex3 - could anyone provide a code example of this amazing routine for me to learn from? Cheers!

Posted by: JSD on August 23, 2008 05:14 PM

gqMu3h comment2 ,

Posted by: Nxnctylm on June 25, 2009 09:23 PM
Post a comment

Email Address:



Remember info?

Thank you!

Most Visited Entries
Sketches, Works & Source Code
In Love with
Powered by
Movable Type 2.661

© Copyright Mario Klingemann

Syndicate this site:
RSS 1.0 - RSS 2.0

Quasimondo @ flickr
Quasimondo @ LinkedIn
Quasimondo @ Twitter
Quasimondo @ Facebook
Quasimondo @ MySpace
Quasimondo is a Bright
Citizen of the TRansnational Republic
My other blog in german

My family name is written Klingemann,
not Klingelmann, Klingeman, Klingaman, Kingemann,
Kindermann, Killingaman, Klingman, Klingmann, Klingonman
Klingemman, Cleangerman, Klingerman or Kleangerman

profile for Quasimondo at Stack Overflow, Q&A for professional and enthusiast programmers