Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:00:18+00:00 2026-06-04T17:00:18+00:00

Let’s say the bottleneck of my Java program really is some tight loops to

  • 0

Let’s say the bottleneck of my Java program really is some tight loops to compute a bunch of vector dot products. Yes I’ve profiled, yes it’s the bottleneck, yes it’s significant, yes that’s just how the algorithm is, yes I’ve run Proguard to optimize the byte code, etc.

The work is, essentially, dot products. As in, I have two float[50] and I need to compute the sum of pairwise products. I know processor instruction sets exist to perform these kind of operations quickly and in bulk, like SSE or MMX.

Yes I can probably access these by writing some native code in JNI. The JNI call turns out to be pretty expensive.

I know you can’t guarantee what a JIT will compile or not compile. Has anyone ever heard of a JIT generating code that uses these instructions? and if so, is there anything about the Java code that helps make it compilable this way?

Probably a “no”; worth asking.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:00:19+00:00Added an answer on June 4, 2026 at 5:00 pm

    So, basically, you want your code to run faster. JNI is the answer. I know you said it didn’t work for you, but let me show you that you are wrong.

    Here’s Dot.java:

    import java.nio.FloatBuffer;
    import org.bytedeco.javacpp.*;
    import org.bytedeco.javacpp.annotation.*;
    
    @Platform(include = "Dot.h", compiler = "fastfpu")
    public class Dot {
        static { Loader.load(); }
    
        static float[] a = new float[50], b = new float[50];
        static float dot() {
            float sum = 0;
            for (int i = 0; i < 50; i++) {
                sum += a[i]*b[i];
            }
            return sum;
        }
        static native @MemberGetter FloatPointer ac();
        static native @MemberGetter FloatPointer bc();
        static native @NoException float dotc();
    
        public static void main(String[] args) {
            FloatBuffer ab = ac().capacity(50).asBuffer();
            FloatBuffer bb = bc().capacity(50).asBuffer();
    
            for (int i = 0; i < 10000000; i++) {
                a[i%50] = b[i%50] = dot();
                float sum = dotc();
                ab.put(i%50, sum);
                bb.put(i%50, sum);
            }
            long t1 = System.nanoTime();
            for (int i = 0; i < 10000000; i++) {
                a[i%50] = b[i%50] = dot();
            }
            long t2 = System.nanoTime();
            for (int i = 0; i < 10000000; i++) {
                float sum = dotc();
                ab.put(i%50, sum);
                bb.put(i%50, sum);
            }
            long t3 = System.nanoTime();
            System.out.println("dot(): " + (t2 - t1)/10000000 + " ns");
            System.out.println("dotc(): "  + (t3 - t2)/10000000 + " ns");
        }
    }
    

    and Dot.h:

    float ac[50], bc[50];
    
    inline float dotc() {
        float sum = 0;
        for (int i = 0; i < 50; i++) {
            sum += ac[i]*bc[i];
        }
        return sum;
    }
    

    We can compile and run that with JavaCPP using this command:

    $ java -jar javacpp.jar Dot.java -exec
    

    With an Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz, Fedora 30, GCC 9.1.1, and OpenJDK 8 or 11, I get this kind of output:

    dot(): 39 ns
    dotc(): 16 ns
    

    Or roughly 2.4 times faster. We need to use direct NIO buffers instead of arrays, but HotSpot can access direct NIO buffers as fast as arrays. On the other hand, manually unrolling the loop does not provide a measurable boost in performance, in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have some json like this in mongo: {n:5} and a java
Let's say I have a method in java, which looks up a user in
Let's say there is a graph and some set of functions like: create-node ::
Let's say I have a bunch of links that share a click event: <a
Let's say I have the string: hello world; some random text; foo; How could
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say I want to have some kind of a cache that did something
Let's say I need to provide a method with a java.lang.Class object that represents
Let's say I don't have photoshop, but I want to make pattern files (.pat)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.