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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:03:44+00:00 2026-05-29T08:03:44+00:00

I am testing performance on integer addition in Java. The way I did that

  • 0

I am testing performance on integer addition in Java. The way I did that is by summing up billions of integers. The sample file I use for testing is a 1G binary file. My program is as simple as shown in the snippet below.

int result = 0;
FileChannel fileChannel = new FileInputStream(filename).getChannel();
long fileSize = fileChannel.size();
intBuffer = fileChannel.map(MapMode.READ_ONLY, startPosition, fileSize).asIntBuffer();

try {
  while (true) {
    result += intBuffer.get();
  }
} catch (BufferUnderflowException e) {
  System.out.println("Complete reading");
}

As you can see from above, it simply executes two operations in each loop

  • read integer from file
  • integer addition

This program ran about 2 minutes on my machine. I also did another test run without addition, by changing result += intBuffer.get() to result = intBuffer.get() (shown as in following snippet).

int result = 0;
FileChannel fileChannel = new FileInputStream(filename).getChannel();
long fileSize = fileChannel.size();
intBuffer = fileChannel.map(MapMode.READ_ONLY, startPosition, fileSize).asIntBuffer();

try {
  while (true) {
    result = intBuffer.get();
  }
} catch (BufferUnderflowException e) {
  System.out.println("Complete reading");
}

The entire program in this case turned out to complete within 1 second. Compared to its sibling variant above, it seems integer addition dominate the CPU time compared to IO read.

I wrote another benchmark program just for justify my guess, it does the same number of additions as the above example.

int result = random.nextInt();
int other = random.nextInt();
int num = 1073741824 / 4;
while(num-- > 0) {
  result += other;
}

With the same amount of integer additions plus the integer incremental operations, this program finishes less than 1 second.

My question is

  • What caused the the major timing difference between these runs? Does Java compiler does something to optimize the last one?

Any thoughts are appreciated.

  • 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-29T08:03:45+00:00Added an answer on May 29, 2026 at 8:03 am

    That’s because disk I/O is very slow compared the CPU.

    In the first case, you’re reading from a file. So you’re bound by disk-access.

    In the second case, it’s all in the CPU.


    So this has nothing to do with the speed of addition.

    • The first case is limited by the speed of your disk.
    • The second case is (probably) limited by the speed of the random number generator.

    As for why result = intBuffer.get() seems to be very fast: (pulled from comments)

    Two possible reasons I can think of:

    • Dead Code Elimination by the JIT is optimizing out all but the last iteration.
    • I/O buffering: The OS is buffering the entire file into memory after the first read.*

    *So subsequent passes will be very fast. It’s easy to test for this case by re-ordering the tests or clearing the I/O cache each time

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

Sidebar

Related Questions

I wrote simple load testing tool for testing performance of Java modules. One problem
While doing performance testing on windows, i usually use perfmon. But when i put
I've been doing some performance testing around the use of System.Diagnostics.Debug, and it seems
We currently use LoadRunner for performance testing our web apps, but we also have
I have some basic questions around understanding fundamentals of Performance testing. I know that
I'm doing performance testing on my iphone app and I'm noticing that sometimes a
I am testing the performance of a data streaming system that supports continuous queries.
I recently did some performance testing and analysis of an ASP.NET application using out-of-process
I'm doing some performance testing of my app and noticed that it takes exceedingly
Does anyone have any thoughts on this method? I have did some performance testing

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.