+ 1
C program CPU usage is very high
Hi! I have implemented delay function in C Programming language. It is working fine but this is using too much CPU power. About 23% But when I used Sleep function from windows.h, CPU usage was Almost 0.0%. I guess this is because of while loop which I have used to delay. Can anyone please tell me how I can make mine delay function better ?? I know many of you will suggest me to use Sleep function from windows api. I already know that I can use that so please kindly tell me the way to make my own function efficient. https://code.sololearn.com/ckbNjy5TZXN6/?ref=app
15 odpowiedzi
+ 8
The sleep function is THE way to suspend your process for a specified time. It is how you communicate to the OS scheduler to prevent CPU usage during that period. Any other technique you might invent will consume more CPU time.
Would you explain more about why your requirement is to not use sleep?
+ 3
I am certain you won't find a way that is independent from the OS.
In Windows you could at least improve on the use of a tight loop timer by letting Windows process other pending events waiting in the event queue. But that is hardly an improvement.
+ 3
C standard lib doesn't do things magically. It's a higher-level abstraction from OS-specific codes.
As others mentioned you need to use OS dependent code when writing something platform-specific or use Assembly (maybe?!).
The following code (C++) is an alternative way to achieve a delay (without a 'busy' loop) with "nearly" the same effect of function ::Sleep().
#include <iostream>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
static void delay(long);
static int init();
static void shutdown();
static HANDLE g_hDelayEvent = nullptr;
int main(int argc, char* argv[])
{
if (!init())
{
std::cout << "failed to init\n";
return 1;
}
int c = 0;
while (1)
{
if (c == 100)
{
break;
}
std::cout << ++c << '\n';
delay(1000);
}
shutdown();
return 0;
}
void delay(long milsec)
{
::WaitForSingleObject(g_hDelayEvent, milsec);
}
int init()
{
if (g_hDelayEvent == nullptr)
{
g_hDelayEvent = ::CreateEventW(nullptr, false, false, nullptr);
}
return g_hDelayEvent == nullptr ? 0 : 1;
}
void shutdown()
{
if (g_hDelayEvent != nullptr)
{
::CloseHandle(g_hDelayEvent);
}
}
+ 2
Some of the components inside this language are very heavy.
----------------
You run the program in dotTrace environment and see which part of the program is using the most resources.
+ 1
Brian
1. It is only available for Windows as it is part of windows api.
2. I wants to learn more
+ 1
Brian
If this is matter of suspending process then I guess I have to use Sleep function.
Or there is another way.
😅😅
+ 1
There is one for Linux although not exactly like the Windows one
https://www.man7.org/linux/man-pages/man3/sleep.3.html
The 2nd answer here tries to make a compatibility bridge
https://stackoverflow.com/questions/1157209/is-there-an-alternative-sleep-function-in-c-to-milliseconds
+ 1
Brian or anyone
Please someone can help me in this ??
https://www.sololearn.com/Discuss/2760339/?ref=app
+ 1
Write a simple C program that would calculate prime numbers and show how long it took to do so, then run the program on a fast modern desktop PC and compare the results.
We quickly came up with this code to count prime numbers:
#include <stdio.h>
#include <time.h>
void main() {
clock_t start, end;
double runTime;
start = clock();
int i, num = 1, primes = 0;
while (num <= 1000) {
i = 2;
while (i <= num) {
if(num % i == 0)
break;
i++;
}
if (i == num)
primes++;
system("clear");
printf("%d prime numbers calculated\n",primes);
num++;
}
end = clock();
runTime = (end - start) / (double) CLOCKS_PER_SEC;
printf("This machine calculated all %d prime numbers under 1000 in %g seconds\n", primes, runTime);
}
+ 1
thanks for the awesome information.
0
Martin Taylor how to do the same thing @Shail did, but in a more efficient way??