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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:22:40+00:00 2026-05-14T14:22:40+00:00

EDIT: I would like to thank you all for the swift replies ^^ Sleep()

  • 0

EDIT:

I would like to thank you all for the swift replies ^^ Sleep() works as intended and my CPU is not being viciously devoured by this program anymore! I will keep this question as is, but to let everybody know that the CPU problem has been answered expediently and professionally 😀

As an aside to the aside, I’ll certainly make sure that micro-optimizations are kept to a minimum in the face of larger, more important problems!

================================================================================

For some reason my program, a console alarm clock I made for laughs and practice, is extremely CPU intensive. It consumes about 2mB RAM, which is already quite a bit for such a small program, but it devastates my CPU with over 50% resources at times.

Most of the time my program is doing nothing except counting down the seconds, so I guess this part of my program is the one that’s causing so much strain on my CPU, though I don’t know why. If it is so, could you please recommend a way of making it less, or perhaps a library to use instead if the problem can’t be easily solved?

/* The wait function waits exactly one second before returning to the *
 * called function.                                                   */     
 void wait( const int &seconds )
 {
 clock_t endwait; // Type needed to compare with clock()
 endwait = clock() + ( seconds * CLOCKS_PER_SEC ); 

 while( clock() < endwait ) {} // Nothing need be done here.
 }

In case anybody browses CPlusPlus.com, this is a genuine copy/paste of the clock() function they have written as an example for clock(). Much why the comment //Nothing need be done here is so lackluster. I’m not entirely sure what exactly clock() does yet.

The rest of the program calls two other functions that only activate every sixty seconds, otherwise returning to the caller and counting down another second, so I don’t think that’s too CPU intensive- though I wouldn’t know, this is my first attempt at optimizing code.

The first function is a console clear using system("cls") which, I know, is really, really slow and not a good idea. I will be changing that post-haste, but, since it only activates every 60 seconds and there is a noticeable lag-spike, I know this isn’t the problem most of the time.

The second function re-writes the content of the screen with the updated remaining time also only every sixty seconds.

I will edit in the function that calls wait, clearScreen and display if it’s clear that this function is not the problem. I already tried to reference most variables so they are not copied, as well as avoid endl as I heard that it’s a little slow compared to \n.

  • 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-14T14:22:40+00:00Added an answer on May 14, 2026 at 2:22 pm

    This:

    while( clock() < endwait ) {} 
    

    Is not “doing nothing”. Certainly nothing is being done inside the while loop, but the test of clock() < endwait is not free. In fact, it is being executed over and over again as fast as your system can possibly handle doing it, which is what is driving up your load (probably 50% because you have a dual core processor, and this is a single-threaded program that can only use one core).

    The correct way to do this is just to trash this entire wait function, and instead just use:

    sleep(seconds);
    

    Which will actually stop your program from executing for the specified number of seconds, and not consume any processor time while doing so.

    Depending on your platform, you will need to include either <unistd.h> (UNIX and Linux) or <windows.h> (Windows) to access this function.

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

Sidebar

Related Questions

I would like to edit matlab scripts in two cases (1) In matlab Command
I would like to place an edit control (WC_EDIT) on a static control (WC_STATIC).
I would like to know how I could edit mp3 files on the server
I would like to write a script which will edit multiple XML files, I
I would like the user to be able to edit the title of the
EDIT : Scroll down to see the updated code. I would like to build
I'm working in Visual Studio 2008 and I would like for Edit > Outlining
I have this line: $(#clients-edit-wrapper).height($(window).height()-150); I would like to apply that height function to
It is possible to group some fields in the edit form? I would like
i would like to access an variable, which is in an class (not as

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.