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 1104307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:27:54+00:00 2026-05-17T01:27:54+00:00

Java. I have a very small int array (4 elements). What’s the fastest way

  • 0

Java.

I have a very small int array (4 elements).

What’s the fastest way to get max value? All values will be between 0 and 255.

Also int array is faster than byte array? Is casting much of a hit?

What’s the fastest way to get sum of all values?

The reason I ask is cause this is a major bottleneck in a program I inherited. They aren’t slow per say, but the guy is calling these methods many many times and it adds up. Eventually I need to rewrite the entire thing, but looking for some quick tricks to speed this up in the short term.

  • 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-05-17T01:27:55+00:00Added an answer on May 17, 2026 at 1:27 am

    Here’s my go at it… basically an unrolled loop. The JIT would probably have recognized that a loop would have gone from 0 to 3 and unrolled it itself anyway, but who knows…

    public static int max(int[] a) {
        int max = a[0] > a[1] ? a[0] : a[1];
        max = a[2] > max ? a[2] : max;
        return a[3] > max ? a[3] : max;
    }
    

    Sum would obviously just be a[0] + a[1] + a[2] + a[3].


    You could also try packing the four 0-255 values in an int, and do some bit-operations instead.

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

Sidebar

Related Questions

I have a very small Java codebase that I would like to expose as
How to obfuscate code quickly. I have a very small Java App and I
I'm very new in Java and I have a small question. I believe it
I have very strange behaviour of Java Date class: System.out.println(new Date().toGMTString()); long l =
I have this very straight forward question regarding Thread and Timer classes in Java
I have a very strange situation. I´m working on a pretty big Java application
I have a piece of code VERY similar to this one http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailFetching I the
I am very new to Android development and Java. Have read around but I'm
I am very new to OSM. I have to build an Java App for
I'm working on a small Java applet, very much new to this programming thing.

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.