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

  • Home
  • SEARCH
  • 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 9119611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:26:16+00:00 2026-06-17T05:26:16+00:00

I’m relatively new to Java and I was curious how Java performed on a

  • 0

I’m relatively new to Java and I was curious how Java performed on a mundane task vs C++. So I compared 2 C++ string formatting methods (sprintf and ostringstream) against using Java’s StringBuilder. For each executable, the first argument I pass is the number of iterations to measure, so all the code looks roughly like

 int numIterations = args[0]; // or argv[1] in C++

 // measure begin here, ie: 
 int begin = System.nanoTime();
 // loop to measure
 for (int i = 0; i < numIterations; ++i)
 {
      // formatting code
 }
 // measure end here, ie
 int end = System.nanoTime();

This lines up with how I’ve seen other people profile Java code. I noticed that for small number of iterations (say 1000), Java does very poorly. For larger number of iterations, (say 1,000,000) Java does relatively well:

With 1000 iterations

$ ./spf.out 1000
C++  took: 1412618 ns
$ ./oss.out 1000
C++  took: 1816222 ns
$ java StringBuilderTest 1000
Java took: 25787951 ns

With 1000000 iterations

$ ./spf.out 1000000
C++  took: 1658699148 ns
$ ./oss.out 1000000
C++  took: 2053606449 ns
$java StringBuilderTest 1000000
Java took:  595965442 ns

I’m suspicious of the method of using begin/end timers to profile java code as I’m not sure exactly how the JITer works. Is this a vaild method of measuring Java code? Does Java have more “warming up” to do when running the code with a JIT’er? What is the canonical way of profiling Java code. Is there a way to warm up the JITer before measuring performance? Or is it just understood that for small N JITing is going to be part of the performance numbers measured?

  • 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-17T05:26:17+00:00Added an answer on June 17, 2026 at 5:26 am

    Is this a vaild method of measuring Java code?

    For the code shown yes. Java is much smarter than C++ at eliminating code which doesn’t do anything. 9 out of 10 benchmarks where Java is significantly faster, this is the case.

    Does Java have more “warming up” to do when running the code with a JIT’er?

    Yes, the warm up goes through stages. Make sure you run the test for 2-10 seconds and it should have been warm enough. You might ignore the first 10K – 20K runs.

    What is the canonical way of profiling Java code. Is there a way to warm up the JITer before measuring performance?

    There is many ways. This is one of the simplest.

     long begin = 0;
     // loop to measure
     for (int i = -11000; i < numIterations; ++i)
        if (i == 0) begin = System.nanoTime();
    

    Or is it just understood that for small N JITing is going to be part of the performance numbers measured?

    It depends on how realistic you want to make your test. If in production the code will not be called 10,000 times and it doesn’t warm up completely, that is what you should measure.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string

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.