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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:50:14+00:00 2026-06-04T07:50:14+00:00

I wrote simple benchmark that test performance of multyplying doubles vs BigDecimal. Is my

  • 0

I wrote simple benchmark that test performance of multyplying doubles vs BigDecimal. Is my method correct? I use randomized values because compiler optimized multyplying constants many times (eg Math.PI * Math.E).
But:
– I don’t know if generating random numbers inside a test corrupts the result.
– The same for creating new BigDecimal objects inside a test.

I want to test performance of multiplication only (not time used by constructor).

How can it be done?

import java.math.*;
import java.util.*;

public class DoubleVsBigDecimal
{
    public static void main(String[] args)
    {
        Random rnd = new Random();
        long t1, t2, t3;
        double t;

        t1 = System.nanoTime();

        for(int i=0; i<1000000; i++)
        {
            double d1 = rnd.nextDouble();
            double d2 = rnd.nextDouble();
            t = d1 * d2;
        }

        t2 = System.nanoTime();

        for(int i=0; i<1000000; i++)
        {
            BigDecimal bd1 = BigDecimal.valueOf(rnd.nextDouble());
            BigDecimal bd2 = BigDecimal.valueOf(rnd.nextDouble());
            bd1.multiply(bd2);
        }

        t3 = System.nanoTime();

        System.out.println(String.format("%f",(t2-t1)/1e9));
        System.out.println(String.format("%f",(t3-t2)/1e9));
        System.out.println(String.format("%f",(double)(t3-t2)/(double)(t2-t1)));
    }
}
  • 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-04T07:50:15+00:00Added an answer on June 4, 2026 at 7:50 am

    You are not only timing the multiply operation, you are also timing other things.

    You need to do something like:

        long time = 0; 
        for(int i=0; i<1000000; i++) {
            double d1 = rnd.nextDouble();
            double d2 = rnd.nextDouble();
            long start = System.nanoTime();
            t = d1 * d2;
            long end = System.nanoTime();
            time += (end-start)
        }
        long meantime = time / 1000000;
    

    then probably calculate the standard error too. Also you will probably need to warm the jvm with some calculations first before you start, otherwise you will get some high values at the start.

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

Sidebar

Related Questions

I wrote simple class that on the start it just increase the value of
I wrote simple load testing tool for testing performance of Java modules. One problem
I wrote a simple function, that makes emacs add matching quotes (so when I
I wrote a simple test program for opencv to see if it's working after
I wrote simple script test echo hello #<-- inside test if I press one
I'm trying to benchmark a simple webserver I wrote, but I'm having trouble deciphering
I wrote simple application that using the cell phone camera. When i trying to
I wrote simple NMEA parser and I'm reading latitude and longitude from GPS. Values
I wrote simple update/insert statements that are returning a syntax error, what am I
I wrote simple application that send some data to my server. Somehow the data

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.