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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:03:35+00:00 2026-06-15T12:03:35+00:00

I have an increasing integer value and want to find the average per second

  • 0

I have an increasing integer value and want to find the average per second of that integer. I am aware that timers do not exist in C unless you do something specific and complicated [Im new to C] Is there a simpler way to do this? Preferably the value would reset when the calculation is made in order to not have such a large number in memory as this application will be running for a long time.

  • 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-15T12:03:36+00:00Added an answer on June 15, 2026 at 12:03 pm

    I think you will want to include time.h, and use some of its functions and structs (this is actually not a bad way of learning the basics of C). There is an explanation and a few examples here.

    If you need sub-second accuracy I suggest you use clock_gettime(), which will give you nanosecond resolution.

    Here is an example:

    #include <stdio.h>
    #include <time.h>
    
    struct timespec diff(struct timespec start, struct timespec end);
    
    int main()
    {
        struct timespec time1, time2, timeDiff;
        int temp, i;
    
        // Get the start time
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
    
        // Do some work
        for (i = 0; i< 242000000; i++)
            temp+=temp;
    
        // Get the end time
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
    
        // Calculate the difference
        timeDiff = diff(time1,time2);
    
        printf("%d.%d\n", timeDiff.tv_sec, timeDiff.tv_nsec);
        return 0;
    }
    
    struct timespec diff(struct timespec start, struct timespec end)
    {
        struct timespec temp;
        if ((end.tv_nsec-start.tv_nsec)<0) {
            temp.tv_sec = end.tv_sec-start.tv_sec-1;
            temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
        } else {
            temp.tv_sec = end.tv_sec-start.tv_sec;
            temp.tv_nsec = end.tv_nsec-start.tv_nsec;
        }
        return temp;
    }
    

    You will need to compile with something like:

    gcc -o timetest timetest.c -lrt
    

    The -lrt part of the command tells the C linker to link to the Realtime library, which contains the definition of clock_gettime().

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

Sidebar

Related Questions

I have a set of integers. I want to find the longest increasing subsequence
I have a large number of records (10,000, increasing every day) that essentially is
I have a method which just increases an Integer Value (i++) public void Calculate()
For my project i need to have custom urls that should not be easy
I have this table: | DAY | TRIMESTER | Day is an integer value,
I have a method within which I need to pass an ever-increasing integer to
I have a list of articles that have increasing ids associated with them. Some
I have a table of increasing dates (with gaps) and corresponding values date |
i have been reading this interesting article which is increasing my every growing confusion
I have this code in PHP writing views to a text-file and just increasing

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.