-
Notifications
You must be signed in to change notification settings - Fork 102
Performance comparison
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 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!
Rehearsal -------------------------------------------------------- RMagick PNG 0.070000 0.020000 0.090000 ( 0.108214) RMagick RGB stream 0.030000 0.030000 0.060000 ( 0.048089) ChunkyPNG PNG 16.900000 0.130000 17.030000 ( 17.401617) ChunkyPNG RGB stream 0.900000 0.030000 0.930000 ( 0.967895) PNG (Seattle.rb) 52.230000 0.320000 52.550000 ( 53.787703) ---------------------------------------------- total: 70.660000sec user system total real RMagick PNG 0.080000 0.020000 0.100000 ( 0.094956) RMagick RGB stream 0.030000 0.030000 0.060000 ( 0.044962) ChunkyPNG PNG 18.290000 0.080000 18.370000 ( 18.706508) ChunkyPNG RGB stream 0.930000 0.040000 0.970000 ( 0.998210) PNG (Seattle.rb) 50.940000 0.300000 51.240000 ( 52.557836)
Rehearsal -------------------------------------------------------- RMagick PNG 0.080000 0.020000 0.100000 ( 0.116996) RMagick RGB stream 0.030000 0.030000 0.060000 ( 0.051755) ChunkyPNG PNG 5.120000 0.060000 5.180000 ( 5.248343) ChunkyPNG RGB stream 0.660000 0.050000 0.710000 ( 0.706412) ----------------------------------------------- total: 6.050000sec user system total real RMagick PNG 0.080000 0.010000 0.090000 ( 0.098726) RMagick RGB stream 0.030000 0.030000 0.060000 ( 0.045542) ChunkyPNG PNG 5.140000 0.050000 5.190000 ( 5.432130) ChunkyPNG RGB stream 0.640000 0.040000 0.680000 ( 0.688764)
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. This is however not always to most efficient method of saving a PNG image, especially when the amount of 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!
Rehearsal ----------------------------------------------------- RMagick PNG 0.390000 0.000000 0.390000 ( 0.404501) RMagick JPG 0.130000 0.000000 0.130000 ( 0.139334) ChunkyPNG fast 1.140000 0.020000 1.160000 ( 1.173626) ChunkyPNG default 2.860000 0.020000 2.880000 ( 2.943260) PNG (Seattle.rb) 1.430000 0.030000 1.460000 ( 1.489315) -------------------------------------------- total: 6.020000sec user system total real RMagick PNG 0.380000 0.000000 0.380000 ( 0.397256) RMagick JPG 0.140000 0.010000 0.150000 ( 0.141685) ChunkyPNG fast 1.050000 0.010000 1.060000 ( 1.093178) ChunkyPNG default 2.790000 0.010000 2.800000 ( 2.865032) PNG (Seattle.rb) 1.420000 0.020000 1.440000 ( 1.496477)
Rehearsal ----------------------------------------------------- RMagick PNG 0.380000 0.010000 0.390000 ( 0.394687) RMagick JPG 0.140000 0.000000 0.140000 ( 0.140492) ChunkyPNG fast 0.400000 0.010000 0.410000 ( 0.423664) ChunkyPNG default 1.330000 0.010000 1.340000 ( 1.351372) -------------------------------------------- total: 2.280000sec user system total real RMagick PNG 0.390000 0.000000 0.390000 ( 0.392915) RMagick JPG 0.140000 0.000000 0.140000 ( 0.142536) ChunkyPNG fast 0.400000 0.010000 0.410000 ( 0.412641) ChunkyPNG default 1.320000 0.000000 1.320000 ( 1.350356)