performance issues with multi-dimensional array

6 views
Skip to first unread message

Amnon

unread,
Nov 24, 2009, 11:30:03 AM11/24/09
to Clojure
I hope it's not the billion time you get the question.
I wanted to use clojure for image processing. I have a 3 dimensional
array I'm passing to clojure from java.
I then loop on the array to manipulate it.
Even the simplest task takes about half a minutes (I expected it to be
over in less than a second)

For example, I am turning an array to gray scale.
I tried to erase all my false improvements and squeeze everything to a
single function:

grey should get a 3Dim array (WxHx3 colors) and return it in a gray
scale.
Any help would be highly appreciated.
Amnon

Konrad Hinsen

unread,
Nov 24, 2009, 1:35:28 PM11/24/09
to clo...@googlegroups.com
On 24 Nov 2009, at 17:30, Amnon wrote:

> I hope it's not the billion time you get the question.
> I wanted to use clojure for image processing. I have a 3 dimensional
> array I'm passing to clojure from java.
> I then loop on the array to manipulate it.
> Even the simplest task takes about half a minutes (I expected it to be
> over in less than a second)

Could you give some more details? For example, what is the Java class
you use for the 3D arrays? How are you accessing it from Clojure? How
do you do the loops? How do you create the destination array, and how
do you fill it with the grey-scale values? I suspect that many of
these steps could be optimized, but it's hard to say without knowing
how your current code works.

Konrad.

Christophe Grand

unread,
Nov 24, 2009, 1:55:54 PM11/24/09
to clo...@googlegroups.com


It would be easier with some of your code. Meanwhile, check this post http://clj-me.cgrand.net/2009/10/15/multidim-arrays/ (it doesn't apply as is to clojure 1.0, you have to replace #^objects by #^"[Ljava.lang.Object;").

Christophe

Amnon

unread,
Nov 25, 2009, 2:55:09 AM11/25/09
to Clojure
Hi Konrad,
In the original post, I put in the code, it was removed by the post
editor.
The way I wanted it implement, I have the main in java (gui and
stuff). I create a 3D array in java, it's an int array (i.e. int arr[]
[][] ) and call with that array to clojure.

It can be done in java at least 3 orders of magnitude faster so I'm
sure there is something I'm doing wrong.
I'll appreciate any suggestion.
Thanks,
Amnon

I've squeezed my example into two functions:

(defn light [arr x y]
(+ (aget arr x y 0) (aget arr x y 1) (aget arr x y 2))
)

(defn grey [arr]
(dotimes [x (length arr)]
(dotimes [y (alength (aget arr 0))]
(let [lg (int (/ (light arr x y) 3))]
(aset arr x y 0 lg)
(aset arr x y 1 lg)
(aset arr x y 2 lg)
))))

David Nolen

unread,
Nov 25, 2009, 1:01:58 PM11/25/09
to clo...@googlegroups.com
Read Christophe's post about multi-dim arrays. Reflection is getting in the way here.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Amnon

unread,
Nov 26, 2009, 5:49:34 AM11/26/09
to Clojure
I did, and it works like a charm. Couldn't figure out how to hint an
array of objects (what should I put instead of the old #^objects?
I still get it to work in 300 ms (which is not great but something I
can live with).
Thanks for the replies,
> > clojure+u...@googlegroups.com<clojure%2Bunsu...@googlegroups.com >

David Nolen

unread,
Nov 27, 2009, 1:06:26 AM11/27/09
to clo...@googlegroups.com

Christophe Grand

unread,
Nov 27, 2009, 5:17:59 PM11/27/09
to clo...@googlegroups.com
On Thu, Nov 26, 2009 at 11:49 AM, Amnon <amnon....@gmail.com> wrote:
I did, and it works like a charm. Couldn't figure out how to hint an
array of objects (what should I put instead of the old #^objects?
I still get it to work in 300 ms (which is not great but something I
can live with).

You have to replace #^objects with #^"[Ljava.lang.Object;" (don't use a more specialized type: it wouldn't help with aget/aset).

 



--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)
Reply all
Reply to author
Forward
0 new messages