+ 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

21st Aug 2021, 1:43 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
12 Answers
+ 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?
21st Aug 2021, 1:55 PM
Brian
Brian - avatar
+ 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.
21st Aug 2021, 2:58 PM
Brian
Brian - avatar
+ 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); } }
22nd Aug 2021, 12:12 PM
Flash
+ 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.
21st Aug 2021, 8:28 PM
Ali shahgholi
Ali shahgholi - avatar
+ 1
Brian 1. It is only available for Windows as it is part of windows api. 2. I wants to learn more
21st Aug 2021, 2:08 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
Brian If this is matter of suspending process then I guess I have to use Sleep function. Or there is another way. 😅😅
21st Aug 2021, 2:23 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 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
21st Aug 2021, 3:25 PM
Ipang
+ 1
Brian or anyone Please someone can help me in this ?? https://www.sololearn.com/Discuss/2760339/?ref=app
21st Aug 2021, 5:50 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 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); }
22nd Aug 2021, 4:55 AM
Arun Jamson
Arun Jamson - avatar
+ 1
thanks for the awesome information.
22nd Aug 2021, 1:10 PM
Alex Sunny
Alex Sunny - avatar
0
Martin Taylor how to do the same thing @Shail did, but in a more efficient way??
22nd Aug 2021, 2:15 AM
Rishi
Rishi - avatar
0
Flash Thanks. I get it. I figured that out by Brian answer. I thought that Sleep function is using time with more efficient manner. And I wanted to know that way. But now I know how Sleep function works. Suspending of process can only be handled by the Operating System. Right ?
22nd Aug 2021, 1:14 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar