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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:21:09+00:00 2026-05-30T17:21:09+00:00

I was wondering if the length of a double variable causes an impact on

  • 0

I was wondering if the length of a double variable causes an impact on the multiplication time. For testing purposes, I wrote the folowing small piece of code:

#include <iostream>
#include <time.h>

int main() {
    double x = 123.456;
    double a = 1.23456;
    double b = 1.234;
    double d = 1.0;

    // experiment 1
    clock_t start = clock();
    for( unsigned long long int i = 0; i < 10000000; ++i ) {
        x *= a;
    }
    clock_t end = clock();
    std::cout << "123.456*1.23456 takes " << (double)(end-start)/CLOCKS_PER_SEC << " secs" << std::endl;

    // experiment 2
    start = clock();
    for( unsigned long long int i = 0; i < 10000000; ++i ) {
        x *= b;
    }
    end = clock();
    std::cout << "123.456*1.234 takes " << (double)(end-start)/CLOCKS_PER_SEC << " secs" << std::endl;

    // experiment 3
    start = clock();
    for( unsigned long long int i = 0; i < 10000000; ++i ) {
        x *= d;
    }
    end = clock();
    std::cout << "123.456*1.0 takes " << (double)(end-start)/CLOCKS_PER_SEC << " secs" << std::endl;

    return 0;
}

I compiled it using VS2008, 64bit in release mode, without optimization and debug information. The result was not surprising: all three kinds of multiplication last exactly the same time, with difference in just a few milliseconds. My question is: why is that so? If I make a mistake and multiply a number by 1.0 instead of 1, and I do not use any compiler optimization, then my multiplication will last much longer than multiplying a number by 1! When humans multiply, then the shorter the number, the faster we come to the result. How does computer multiply so that it does not matter, how long the two numbers are?

Apart from that, I decided to check whether debugging influences runtime speed. In this case, it does not: compiling with \DEBUG option or without, multiplication always takes exactly the same amount of time.

With optimization enabled \O2, the same multiplication lasts only a thousandth of a second. What does optimization do in this case? How can such a compact code of multiplying two doubles in C++ be optimized?

I would be grateful for any explanation, what happens during double multiplication in C++.

  • 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-30T17:21:11+00:00Added an answer on May 30, 2026 at 5:21 pm

    The variables have always the same length, but the values differ. With other words: the operations to perform at hardware level are the very same, thus resulting. E.g., an integer, multiplying by 0 (zero out result, i.e. transfer 0s into destination register) takes the same time as multiplying by 1 (copy operand into destination register).

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

Sidebar

Related Questions

I'm wondering if variable length lookbehind assertions are supported in JavaScript's RegExp engine? For
I'm wondering if there is some way to unpack a variable length argument list
I'm wondering for some time now why this fails: if (longest.length >= 3) {
I'm wondering if string.Length in C# is an instant variable. By instant variable I
I was wondering the length of App ID and Registration ID, and Device ID
In Java, I'm wondering why the length attribute of the String class isn't private?
I was wondering how one can fix an upper limit for the length of
I was wondering, what is the maximum possible length of a CISC instruction on
I was wondering if there's a way to get the length of a second-level
Just wondering what that error could mean? I get it from the following code..

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.