Performance comparison

wvanbergen edited this page Aug 18, 2010 · 4 revisions

Being a pure Ruby library, this library is bound to be slower than a native implementation, like RMagick. It is interesting to see how it compares to Seattle.rb’s PNG, another “almost” pure Ruby PNG library (albeit rather under-maintained). I have done some benchmarks to give an indication of the relative performance.

For these tests, I have used ChunkyPNG 0.5.2, RMagick 2.12 and PNG 1.2, using both Ruby 1.8.7 and Ruby 1.9.1. Using Ruby 1.9 will cause a significant speed boost, simply because the algorithms are executed faster. Unfortunately, the Seattle.rb PNG library is currently not compatible with Ruby 1.9.

Reading

Reading a PNG file is currently the slowest operation in ChunkyPNG, because of the many calculations that are needed. I have benchmarked loading a 240×180, RGB image 50 times, using RMagick, ChunkyPNG and Seattle.rb’s PNG.

It is also possible to load an image using an unencoded RGB stream. This is much faster, because almost no decoding is needed for this. I have included the benchmarks for this loading method for RMagick and ChunkyPNG, using the same image. PNG does not support this.

Note that using Ruby 1.9.1. makes decoding a PNG file over 3 times faster when using ChunkyPNG!

n = 50                 Ruby 1.8.7   Ruby 1.9.1
----------------------------------------------
RMagick PNG              0.095130     0.116996
RMagick RGB stream       0.044009     0.051755
ChunkyPNG PNG           17.970393     5.248343
ChunkyPNG RGB stream     0.988422     0.688764
PNG (Seattle.rb)        52.485190          n/a

Writing to memory

Writing a PNG image is a much faster. In this benchmark, the same 240×180 image is saved to a memory blob 50 times. I have included saving as a JPEG image using RMagick for comparison.

ChunkyPNG is highly optimized to save PNG images using RGB encoding. Unfortunately, this is however not always to most efficient method of saving a PNG image, especially when the amount of used colors is low. By default, ChunkyPNG will try to save using the most efficient encoding. Figuring this out requires some time, because the image’s palette has to examined. By passing :fast_rgb or :fast_rgba as parameter to the to_blob or save method, ChunkyPNG will skip this step and always use RGB or RGBA encoding respectively.

Note that using Ruby 1.9.1 improves performance significantly when writing, too!

Ruby 1.8.7

n = 50                 Ruby 1.8.7   Ruby 1.9.1
----------------------------------------------
RMagick PNG              0.397256     0.392915
RMagick JPG              0.141685     0.142536
ChunkyPNG fast           1.093178     0.412641
ChunkyPNG default        2.865032     1.350356
PNG (Seattle.rb)         1.496477          n/a