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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:11:44+00:00 2026-06-13T04:11:44+00:00

The attached program (see at the end), when executed, yields the following output: ……….

  • 0

The attached program (see at the end), when executed, yields the following output:

..........
with sleep time of 0ms
  times= [1, 1, 1, 0, 1, 1, 0, 1, 1, 0]
  average= 0.7
..........
with sleep time of 2000ms
  times= [2, 2, 2, 2, 2, 1, 2, 2, 2, 2]
  average= 1.9

In both cases the exact same code is executed which is to repeatedly get the next value from a Random object instantiated which at the start of the program. The warm up method executed first is supposed to trigger any sort of JIT otimizations before the actual testing begins.

Can anyone explain the reason for this difference? I have been able to repeat this result in my machine every time so far, and this was executed on a multi-core Windows system with java 7.

One interesting thing is that if the order in which the tests are executed is reversed, that is, if we run the loop with the delay before the loop without the delay, then the timings are more similar (with the no delay loop actually taking longer):

..........
with sleep time of 2000ms
  times= [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
  average= 2.0
..........
with sleep time of 0ms
  times= [2, 3, 3, 2, 3, 3, 2, 3, 2, 3]
  average= 2.6

As much as I could tell, no object is being created inside the operation method, and when running this through a profiler it does not seem that garbage collection is ever triggered. A wild guess is that some value gets cached in a processor-local cache which gets flushed out when the thread is put to sleep and then when the thread wakes up it needs to retrieve the value from main memory, but that is not so fast. That however does not explain why inverting the order makes a difference…

The real-life situation where I initially observed this behavior (which prompted me to write this sample test class) was XML unmarshalling, where I noticed that unmarshalling the same document repeated times one after the other in quick succession yielded better times than performing the same thing but with a delay between calls to unmarshal (delay generated through sleep or manually).

Here is the code:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Tester
{
    public static void main(String[] args) throws InterruptedException
    {
        warmUp(10000);

        int numRepetitions = 10;
        runOperationInALoop(numRepetitions, 0);
        runOperationInALoop(numRepetitions, 2000);
    }

    private static void runOperationInALoop(int numRepetitions, int sleepTime) throws InterruptedException
    {
        List<Long> times = new ArrayList<Long>(numRepetitions);
        long totalDuration = 0;

        for(int i=0; i<numRepetitions; i++)
        {
            Thread.sleep(sleepTime);

            long before = System.currentTimeMillis();
            someOperation();
            long duration = System.currentTimeMillis() - before;

            times.add(duration);
            totalDuration = totalDuration + duration;

            System.out.print(".");
        }
        System.out.println();

        double averageTimePerOperation = totalDuration/(double)numRepetitions;

        System.out.println("with sleep time of " + sleepTime + "ms");
        System.out.println("  times= " + times);
        System.out.println("  average= " + averageTimePerOperation);
    }

    private static void warmUp(int warmUpRepetitions)
    {
        for(int i=0; i<warmUpRepetitions; i++)            
        {
            someOperation();
        }
    }

    public static int someInt;
    public static Random random = new Random(123456789L);

    private static void someOperation()
    {
        for(int j=0; j<50000; j++)
        {
            someInt = ((int)random.nextInt()*10) + 1;
        }
    }
}
  • 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-13T04:11:45+00:00Added an answer on June 13, 2026 at 4:11 am

    When you sleep for even a short period of time (you may find that 10 ms is long enough) you give up the CPU and the data, instruction and branch prediction caches are disturbed or even cleared. Even making a system call like System.currentTimeMillis() or the much more accurate System.nanoTime() can do this to a small degree.

    AFAIK, The only way to avoid giving up the core is to busy wait and using thread affinity to lock your thread to a core. This prevent minimises such a disturbance and means your program can runs 2-5x faster in low latency situations i.e. when sub-millisecond tasks matter.

    For your interest

    http://vanillajava.blogspot.co.uk/2012/01/java-thread-affinity-support-for-hyper.html

    http://vanillajava.blogspot.co.uk/2012/02/how-much-difference-can-thread-affinity.html

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

Sidebar

Related Questions

I have a Windows Mobile program that accesses an attached device through a third-party
(see attached jsfiddle, screenshots; running browsers on Ubuntu 11.10) There seems to be some
I attached to a program with gdb in OSX and I want to use
Attached is a quick program written under interview conditions that is designed to flatten
I'm writing a program looking for 'herenow' of Foursquare. As I attached, there are
I had used the same plot as attached, for another question. One could see
Is it possible to program a wireless adapter attached to a computer? I need
I run into the following problem.I load my shaders from files.The shader program ,when
The attached, trivial, test program tests the performance of emptying a simple std::map. Using
My Perl program is reading data from a serial device attached through USB. Headlines

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.