How efficient is coding objects using Java?

Oct 24, 2007 at 2:54pm

How efficient is coding objects using Java?

Hi,

I have just written an MSP pitch detector object in java. It works perfectly but has a very high load on the CPU. In fact, if I use a signal vector size of more than 256 it causes clicking artefacts in the audio. The code isn’t really that big and I am using a brand new intel mac book 2.2 GHz machine.

Is this because of some speed limitation of using mxj quickie to build java objects? How could I improve the speed?

I am normally a C programmer and have coded much much larger things that run in real-time as VST plug-ins for example – and they generally are not that much of a burden on the CPU. I know Java is slower than C but I didn’t expect it to be quite so limiting, is there something I am doing wrong?

Alternatively, if anyone could point me to a good introduction to programming Max/MSP objects in C I would be very grateful (apologies as this question prob should go in the other forum but the first question should be here…).

I’d be very happy if someone could point me in the right direction,

Many thanks,

Adam

#34303
Oct 24, 2007 at 5:15pm

Java is definitely slower than C but it is possible
with some care to get it running pretty efficiently.

I could take a look at your code if you want. Things to avoid
are unnecessary object allocation, method call overhead,
etc. Generally having a knowledge of what the JVM will do
when it executes certain instructions.

I generally tend to think as if I am writing C code when writing
mxj~ stuff and stay as much as possible with the primitive constructs
of the java language.

Hope this is helpful.

Topher

On Oct 24, 2007, at 07:54 AM, Adam Stark wrote:

>
> Hi,
>
> I have just written an MSP pitch detector object in java. It works
> perfectly but has a very high load on the CPU. In fact, if I use a
> signal vector size of more than 256 it causes clicking artefacts in
> the audio. The code isn’t really that big and I am using a brand
> new intel mac book 2.2 GHz machine.
>
> Is this because of some speed limitation of using mxj quickie to
> build java objects? How could I improve the speed?
>
> I am normally a C programmer and have coded much much larger things
> that run in real-time as VST plug-ins for example – and they
> generally are not that much of a burden on the CPU. I know Java is
> slower than C but I didn’t expect it to be quite so limiting, is
> there something I am doing wrong?
>
> Alternatively, if anyone could point me to a good introduction to
> programming Max/MSP objects in C I would be very grateful
> (apologies as this question prob should go in the other forum but
> the first question should be here…).
>
> I’d be very happy if someone could point me in the right direction,
>
> Many thanks,
>
> Adam

#115476
Oct 24, 2007 at 5:23pm

also…

Using the benchmark message allows you to quantify any optimizations
that you might be making
and could be helpful if you are not aware of it. See the mxj~
examples for use of the benchmark
message.

T

On Oct 24, 2007, at 07:54 AM, Adam Stark wrote:

>
> Hi,
>
> I have just written an MSP pitch detector object in java. It works
> perfectly but has a very high load on the CPU. In fact, if I use a
> signal vector size of more than 256 it causes clicking artefacts in
> the audio. The code isn’t really that big and I am using a brand
> new intel mac book 2.2 GHz machine.
>
> Is this because of some speed limitation of using mxj quickie to
> build java objects? How could I improve the speed?
>
> I am normally a C programmer and have coded much much larger things
> that run in real-time as VST plug-ins for example – and they
> generally are not that much of a burden on the CPU. I know Java is
> slower than C but I didn’t expect it to be quite so limiting, is
> there something I am doing wrong?
>
> Alternatively, if anyone could point me to a good introduction to
> programming Max/MSP objects in C I would be very grateful
> (apologies as this question prob should go in the other forum but
> the first question should be here…).
>
> I’d be very happy if someone could point me in the right direction,
>
> Many thanks,
>
> Adam

#115477
Oct 25, 2007 at 1:19pm

Hi Topher,

Thanks for your replies. It would be great if you could have a look at my code. I have attached the java file. The function causing the problems is the pitch_YIN function.

I also have one more question which is about C coding Max/MSP objects – so far, all the examples I have seen so far in C have been massively complex, uncommented and unexplained. The Java equivalent is so simple. Is there a simple C example somewhere or a good guide? Or is it generally complicated?

I’m very very appreciative of your help,

Thanks

Adam

#115478
Oct 25, 2007 at 1:45pm

Hola
I am about to get on a plane here but will check the code in the next
day or so.

The c external api is covered in writing max/msp externals by david
zicarelli.

It is not overly complicated. Just a little different.

I agree that there could be a higher level c api for externs. I was
working on a similar object to mxj called cfunk that addresses these
issues by allowing you to write in a similar way but in c instead of
java .I have stopped development of cfunk but perhaps I will finish it
up someday or someone else is interested in taking it over.

#115479
Oct 26, 2007 at 3:44pm

hi adam,
the reason why your code is slow is not due to the language (at least
not entirely), but to the fact that you try to calculate
the square difference function in the time domain. in the nested for-
loops you are doing a lot of calculations…
this wouldn’t be efficient in C either.
but using FFT you can easily calculate the auto-correlation much
quicker.
i believe that’s the trick other yin-pitchtrackers use.

volker.

On 25 Oct 2007, at 15:19, Adam Stark wrote:

> Hi Topher,
>
> Thanks for your replies. It would be great if you could have a look
> at my code. I have attached the java file. The function causing the
> problems is the pitch_YIN function.
>
> I also have one more question which is about C coding Max/MSP
> objects – so far, all the examples I have seen so far in C have
> been massively complex, uncommented and unexplained. The Java
> equivalent is so simple. Is there a simple C example somewhere or a
> good guide? Or is it generally complicated?
>
> I’m very very appreciative of your help,
>
> Thanks
>
> Adam
>

#115480
Oct 26, 2007 at 4:02pm

Hi volker,

Yes, that is a good suggestion and would make it run much faster. I also found another problem. This is maybe very obvious to more experienced Java users but I am new to using Java for signal processing: I found that, in my loop, when I calculated the square of a number using:

Math.pow(x,2);

my function took 19ms to do it’s calculations. However, when I used:

x*x

the processing time was reduced to just 3ms.

Anyway, thank you everyone for your help,

Adam

#115481
Oct 26, 2007 at 4:19pm

#115482
Oct 26, 2007 at 4:35pm

I still haven’t found a spot to view your attachment but that makes
sense. Method call overhead in tight loops adds up and the java Math
library is typicaly slow on top of that. In the mxj~ noise examples you
wil see I opted to write my own random fucntion instead of using
Math.nextfloat. It seems like you are able to make use of the
benchmarking stuff as well. Kudos.

#115483

You must be logged in to reply to this topic.