Archived

This topic is now archived and is closed to further replies.

Sleep() without windows.h

This topic is 5676 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a win32 console program and after compiling it to try it out, i noticed that it is huge (over 90kb for only ~750 lines of code)! I looked through and noticed i was including windows.h, but the only function i was using from it is Sleep(). So to save space, i want to implement Sleep() on my own, without using windows.h. I was wondering how might i go about this? is there anyway to get at some sort of system or internal clock, or maybe some kind of timer, without using windows.h? ------------------------------ BASIC programmers don''t die, they just GOSUB and don''t return.

Share this post


Link to post
Share on other sites
I''m pretty sure Sleep doesn''t add to your exe size. Comment it out and rebuild; you''ll most likely get about 90 kb anyway.

There have been several threads lately on reducing exe file sizes, so visit the search page.

Share this post


Link to post
Share on other sites
Sleep relinquishes your time slice to other threads - the best you could do without it, is spin in a hard-loop (which using 100% of the CPU as opposed to ~0% the sleep consumes).

Are you compiling a release build? The smallest exe Visual C++ seems to make is about 40k. If you want to go smaller than that, you can write it in assembly using MASM.

The way C/C++ works, functions aren''t linking into your exe unless you use them. So including windows.h shouldn''t add anything you''re not using. In order to call a function like Sleep, the system needs to load a dll - which is different depending on the OS (9x compared to NT). It''s unlikely you''d see a size decrease if you wrote your code to hook up to the sleep.

Share this post


Link to post
Share on other sites
Can you use time() in time.h?

It says it returns elapsed time, you should be able to look it up yourself by typing time into the index for MSDN and looking at the Vis C++ programmer's guide.

(from MSDN lib for VC++ 6)

time
Gets the system time.

time_t time( time_t *timer );

Routine Required Header
time time.h

Return Value

time returns the time in elapsed seconds. There is no error return.


[edited by - Tebriel on June 2, 2002 12:51:30 AM]

Share this post


Link to post
Share on other sites
I am using Dev-C++ and I have #defined WIN32_LEAN_AND_MEAN.

------------------------------
BASIC programmers don''t die, they just GOSUB and don''t return.

Share this post


Link to post
Share on other sites
Here is a great little thing I picked up for doing things such as pausing or sleeping, it uses somthing called 64 Bit High Performace Counter, this uses your cpu for timming, basicaly it just sends a return value a certian ammount of times per second, my 1.6 gHz sends 3,292,187 per second. Anyways here is the code:

  
void Pause(int MilliSeconds)
{
//Convert the miliseconds into seconds

INT64 secsWait = (INT64)MilliSecs * 1000;
INT64 timeStart;
INT64 timeNow;

//Get the time the timer starts at

QueryPerformanceCounter((LARGE_INTEGER*)&timeStart);

while (true)
{
//Get the current time

QueryPerformanceCounter((LARGE_INTEGER*)&timeNow);

//If there has been the required time passing then stop

if ((timeNow - timeStart) > (INT64)secsWait)
break;
}
}

Hope this helps

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
The smallest exe Visual C++ seems to make is about 40k. If you want to go smaller than that, you can write it in assembly using MASM.


Or you can reduce segment alignment, merge segments, and use freeware executable packers such as UPX. The same is true for MASM.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
what''s the big deal? Your computer not have 90kb of ram or something? This issue is just distracting you, get back to coding and finish your project.

Share this post


Link to post
Share on other sites
Chopped straight out of my code (may be MSVC specific, but Dev-C++ won''t complain if it is):

//----------- optimizations ----------------

/*
OPTIMIZATION STATISTICS
-----------------------
-NONE: 76.0 KB
-w/o ALLIGN 72.0 KB ( 8.0 KB)
-w/o GSY: 67.5 KB ( 1.5 KB)
-w/o MERGE: 66.5 KB ( 0.5 KB)
-ALL: 66.0 KB (10.0 KB)
*/

#pragma optimize("gsy", on)
#pragma comment(linker, "/merge:.text=.data")
#pragma comment(linker, "/FILEALIGN:0x200")

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

Share this post


Link to post
Share on other sites