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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:36:39+00:00 2026-05-25T10:36:39+00:00

So I have this Java program that I use to munch through several terabytes

  • 0

So I have this Java program that I use to munch through several terabytes of data. Performance is a concern.

I’ve profiled the app, and a large fraction of all memory allocations as well as a large fraction of CPU time come from performing one simple operation:

I have an array of ASCII chars. I know that the characters from offset i to offset j represent a floating-point number. I need to extract that floating-point number into a double.

The naive Double.parseDouble(new String(buf, i, j - i)) does the job. However, this is where a lot of time is spent and a lot of memory allocations come from, probably because:

  • new String() creates a new object, creates an internal char[] array
    and copies the characters into the array;
  • Double.parseDouble()
    creates a FloatingDecimal object, and too creates a char[] array,
    also copying the characters into it.

All these allocations and all this copying are not really necessary. Can I avoid them?

What I’d really like is a strtod-like function that would take a char[] (or a byte[]) as well as start/end offsets, and return a double.

Any suggestions? Should I roll out my own? Should I write a JNI wrapper around strtod? Should I use some Java library that’s already out there?

  • 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-25T10:36:40+00:00Added an answer on May 25, 2026 at 10:36 am

    What I have done in the past is write a parser for ByteBuffer (to avoid byte to char encoding conversion) to double and visa-versa. If you can avoid creating any objects it can be much faster. This approach works for memory mapped files avoiding some copy costs as well.

    The core code looks like the following. It doesn’t handle exponents, but you can add that.

    @Override
    public double read() throws BufferUnderflowException {
      long value = 0;
      int exp = 0;
      boolean negative = false;
      int decimalPlaces = Integer.MIN_VALUE;
      while (true) {
        byte ch = buffer.get();
        if (ch >= '0' && ch <= '9') {
          while (value >= MAX_VALUE_DIVIDE_10) {
            value >>>= 1;
            exp++;
          }
          value = value * 10 + (ch - '0');
          decimalPlaces++;
        } else if (ch == '-') {
          negative = true;
        } else if (ch == '.') {
          decimalPlaces = 0;
        } else {
          break;
        }
      }
    
      return asDouble(value, exp, negative, decimalPlaces);
    }
    

    The full code

    It stops as soon as it gets any byte it doesn’t expect e.g. a , or \n

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

Sidebar

Related Questions

Suppose that I have a Java program within an IDE (Eclipse in this case).
I inherited this gigantic legacy Java web app using Struts 1.2.4. I have a
I inherited this gigantic legacy Java web app using Struts 1.2.4. I have a
I have written a java annotation that looks like this: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) // can
I have to replace the content of this xml string through java <My:tag>value_1 22&#xA;value_2
I have a Java program that is launched by a batch file with a
I have a java program that uses a weak hashmap for caching some things,
I have this java code: <script src=http://www.google.com/jsapi></script> <script type=text/javascript> google.load(jquery, 1.2.6); $(a#more).click(function() { $(#info_box).show(blind,
I have this java string: String bla = <my:string>invalid_content</my:string>; How can I replace the
I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException

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.