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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:55:24+00:00 2026-05-17T21:55:24+00:00

I have the following C99 program which measures performance of simple division operations relative

  • 0

I have the following C99 program which measures performance of simple division operations relative to addition. However, the difftime function keeps returning 0 even though the program is clearly taking several seconds to process runAddition and runDivision with iterations set to 1 billion.

#include <stdio.h>
#include <time.h>

void runAddition(long long iterations)
{
    long long temp;
    for (long long i = 1; i <= iterations; i++)
    {
        temp = temp + i;
    }
}

void runDivision(long long iterations)
{
    long long temp;

    // Start at 1 to avoid division by 0!
    for (long long i = 1; i <= iterations; i++)
    {
        temp = temp / i;
    }
}

int main()
{
    long long iterations = 1000000000;
    time_t startTime;

    printf("How many iterations would you like to run of each operation? ");
    scanf("%d", &iterations);

    printf("Running %d additions...\n", iterations);
    startTime = time(NULL);
    runAddition(iterations);
    printf("%d additions took %f seconds\n", iterations, difftime(time(NULL), startTime));

    printf("Running %d divisions...\n", iterations);
    startTime = time(NULL);
    runDivision(iterations);
    printf("%d divisions took %f seconds\n", iterations, difftime(time(NULL), startTime));
}
  • 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-17T21:55:24+00:00Added an answer on May 17, 2026 at 9:55 pm

    Your format string expects an int (%d), and a double (%f). Your arguments are long long and double. You should set the first format string as %lld.

    When pushing arguments on the stack to call printf, you push a long long using 8 bytes, and a double using 8 bytes too. When the function printf reads the format string, it expects first an int on 4 bytes, and a double on 8 bytes. printf gets the int correctly as you are little-endian and the first four bytes of your long long are enough to represent the value. printf then gets the double for which it gets the last four bytes of the long long, followed by the first four bytes of the double. As the last four bytes of the long long are zeroes, what printf thinks is a double starts with four bytes with value zero, resulting in a very very tiny value for the double according to the binary representation of doubles.

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

Sidebar

Related Questions

I have following problem: My program should decide at runtime to load an function
I have the following code which you can try using c99 filename.c; ./a.out #include
gcc 4.1.2 c99 I have the following enum's in this file ccsmd.h : enum
I have following Code Block Which I tried to optimize in the Optimized section
I have following lines of code in a program VisitWebsiteVC *visitWebSite = [[VisitWebsiteVC alloc]
I have written the following C99 code and was wondering about the struct declaration.
I have following Linq query/function in my MVC3 application. public AuditTrail GetNamesAddressesEmployers(long registryId ,
I have following procedure which is filling up null values in a column. The
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other 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.