Switch branches/tags
Nothing to show
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


FreeImage interface to D and performance comparison with Python
    - Ankit Rohatgi

SEE DISCUSSION: http://www.reddit.com/r/programming/comments/2tp7a3/image_processing_in_d_vs_python_a_quick_and_dirty/

What? Why?

This code contains D interface to C functions that provide access to some of the capabilities of the FreeImage library. I could not get any of the existing bindings to FreeImage to work (e.g. derelict-fi), so I decided to expose only the basic functions that were useful to me. I was happy to find that it was extremely easy to expose C functions to D and so I did not see the utility in creating an interface to the entire library.

I have also compared the performance of this interface with Python via a simple test. In the test, 1000 PNG images are created and read back to calculate an average RGB value of all the pixels in each image. 

It was found that the D code was about 3-4 times faster when compared to the Python code running with the default Python interpreter (However, it was pointed out on Reddit, that the use of numpy.mean() could have resulted in a much quicker computation). With PyPy, Python code was slightly faster than D. It was pointed out on Reddit that using GDC instead of DMD could have resulted in faster runtimes for D code as well. In conclusion, I think both D and Python approaches are comparable in performance. Using correct optimizations and interpreter (PyPy) makes a huge difference in the outcome.

Performance Test

1000 PNG images of size 640x480 pixels are created sequentially and also in parallel. After creating, the images are immediately read back to calculate average RGB value in each image by accessing each pixel.

Results (All times are in seconds)

1. My Desktop: i5-4690K (4 Cores)

                D       Python      PyPy
    Serial      24      63          15
    Parallel     6      17           4

        - CPU: Intel i5-4690K (4 cores, no OC)
        - RAM: 8 GB 1600 MHz
        - OS: x86_64 Fedora 21
        - HDD Speed: 7200 RPM 

2. My Laptop: Late 2011 Macbook Pro - i5-2435M (2 cores, 4 with hyperthreading)
                D       Python      PyPy
    Serial      33      154         27
    Parallel    17      78          12

        - CPU: Intel i5-2435M (2 cores)
        - RAM: 8 GB 1333 MHz
        - OS: Mac OS 10.10.1
        - HDD Speed: Samsung 840 SSD  

Code Layout

    - cfreeimage.c - C functions that make the calls to FreeImage functions.
    - freeimage.d - D Module that defines a FreeImage class. Methods of this class call the C functions defined above.
    - main.d - Entry point into the D code, performance test
    - python.py - Python code for performance comparison.


    - D Programming Language: http://dlang.org
    - Interfacing D to C: http://dlang.org/interfaceToC.html
    - FreeImage Library: http://freeimage.sourceforge.net
    - A pre-existing FreeImage binding to D: https://github.com/DerelictOrg/DerelictFI (This is probably what I should have used).
    - PyPy: http://pypy.org/ 


    Public Domain! Suggestions Welcome!