Vector API performance

Remi Forax forax at univ-mlv.fr
Fri May 26 06:30:28 UTC 2023


Hi Bruno,
as Kasper says, you need to use JMH when you do a perf test. Java does not work like C, C++ or Go. The code optimization is not done at compile time but at runtime (see  JIT [1]), so when benchmarking you have wait until the code optimizers have run. That's why we are using JMH [2] to test code performance in Java.

For your second question, when you read the data from an array to a vector, you load all the data at once, so you need enough data in the array to fill the vector.
To solve that, you need a post loop as explained in the javadoc [3]. 

regards,
Rémi

[1] https://en.wikipedia.org/wiki/Just-in-time_compilation
[2] https://www.baeldung.com/java-microbenchmark-harness
[3] https://docs.oracle.com/en/java/javase/20/docs/api/jdk.incubator.vector/jdk/incubator/vector/package-summary.html

----- Original Message -----
> From: "Kasper Nielsen" <kasperni at gmail.com>
> To: "Bruno Rafael Sant Ana" <bruno.santana.ti at gmail.com>
> Cc: "panama-dev" <panama-dev at openjdk.org>
> Sent: Friday, May 26, 2023 8:04:52 AM
> Subject: Re: Vector API performance

> Hi,
> 
> You cannot reliable benchmark code on the JVM as you do in your example.
> As you are also measuring things such as VM startup, code compilation,
> and lots of other things in addition to your own code.
> 
> Instead, you should rewrite your benchmarks using JMH.
> https://github.com/openjdk/jmh
> Which is the defacto micro benchmarking tool for the JVM.
> You can find loads of articles and videos about the tool if you search.
> 
> Best,
>  Kasper
> 
> 
> 
> On Fri, 26 May 2023 at 04:24, Bruno Rafael Sant Ana
> <bruno.santana.ti at gmail.com> wrote:
>>
>> Hi everyone,
>>
>> This is the first time I'm using a Java mailing list, so hopefully I'm doing it
>> correctly.
>>
>> I've heard that the Vector API should increase the performance when the computer
>> has a SIMD architecture.
>>
>> I wrote this code to test the Vector API and surprisingly it seems to be slower
>> than the traditional way:
>>
>> https://github.com/brunosantanati/java-new-features/blob/main/src/main/java/me/bruno/santana/incubator/VectorApiDemo.java
>>
>> I got 5969 ns (traditional way) VS 48475736 ns (Vector API code). Why is that?
>> Maybe my laptop has no SIMD capabilities?
>>
>> In addition to that, I have one more question. When I'm using the
>> IntVector.fromArray method passing an array with length less than 16 I get an
>> IndexOutOfBoundsException. Why is that?
>>
>> Thank you!
>>
>> --
>>
>> "Any fool can write code that a computer can understand. Good programmers write
> > code that humans can understand." Martin Fowler


More information about the panama-dev mailing list