๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ดˆ๋ณด๊ฐ€ ๊ฒช๋Š” ๋ฌธ์ œ๋“ค

C์–ธ์–ด ์†๋„ ์ธก์ •์šฉ ํ…œํ”Œ๋ฆฟ

๐ŸŽฎinspirer9 2021. 2. 6. 01:14
728x90
๋ฐ˜์‘ํ˜•

C์–ธ์–ด๋กœ ์ง  ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ์–ผ๋งˆ๋‚˜ ๋น ๋ฅผ๊นŒ ๊ถ๊ธˆํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค.

(์™œ ๊ถ๊ธˆํ•ด์•ผ ํ•˜๋ƒ๊ณ ?)

๊ทธ...๊ทธ๊ฑฐ์•ผ ๊ฒŒ์ž„ ๋งŒ๋“œ๋‹ˆ๊นŒ...

์•„ ์ด์   ํ•„์š”์—†์ง€? ์š”์ฆ˜์€ ์ปดํ“จํŒ… ํŒŒ์›Œ๊ฐ€ ๊ดด๋ฌผ์ด๋ผ์„œ (์‹œ๋ฌด๋ฃฉ)

 

์•”ํŠผ ๊ทธ๋ž˜๋„ ๋‚œ ๊ถ๊ธˆํ•˜๋‹ค.

์†Œํ”„ํŠธ์›จ์–ด์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•  ๋•Œ ์ผ๋ฐ˜์ ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•œ ๋‹ค์Œ์— (2)-(1)ํ•ด์„œ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์„ ์ธก์ •ํ•œ๋‹ค.

// ์ž‘์—… ์‹œ์ž‘ ์‹œ๊ฐ ์ €์žฅ(1)
// ์ž‘์—…
// ์ž‘์—… ์ข…๋ฃŒ ์‹œ๊ฐ ์ €์žฅ(2)

 

<time.h>์˜ clock()

์ฝ”๋“œ๋กœ ๋ณด๋ฉด ์ด๋Ÿฐ ์‹์ด๋‹ค.

#include <stdio.h>
#include <time.h>

main()
{
    clock_t start,end;
    start = clock();
    
    /* ๋ฃจํ”„ ์‹œ์ž‘ */
    int j;
    for(int i=0; i<100000000;i++) { j += j + i; }
    /* ๋ฃจํ”„ ์ข…๋ฃŒ */
    
    end = clock(); 
    printf("Exeution period is %.3f sec.\n",(double)(end-start)/CLOCKS_PER_SEC);
}

 

<time.h>์˜ gettimeofday()

์ข€ ๋” ๊น”๋”ํ•œ ๋ฐฉ๋ฒ•

cTimer timer; // ํƒ€์ด๋จธ ํด๋ž˜์Šค
timer.start(); // ์ž‘์—… ์‹œ์ž‘ ์‹œ๊ฐ ์ €์žฅ(1)
//์ž‘์—…
timer.check(); // ์ž‘์—… ์‹œ๊ฐ„ ๊ณ„์‚ฐ (2-1)

cTimer๋Š” time.h์— ์žˆ๋Š” gettimeofday()๋ฅผ ์ด์šฉํ–ˆ๋‹ค.

#include <sys/time.h>
#include <time.h>
#include <stdio.h>

class cTimer
{
    struct timeval timeBegined;
    struct timeval timeEnded;

public:
    void start()
    {
        gettimeofday(&timeBegined, NULL);
    }

    void check()
    {
        gettimeofday(&timeEnded, NULL);

        static char     time_str[64];
        time_t          *p_time;
        struct tm       *p_tm;

        int a;

        p_time = (time_t *)&(timeEnded.tv_sec);
        p_tm = localtime(p_time);
        sprintf( time_str, "%02d%02d%02d.%03ld",
            p_tm->tm_hour,      p_tm->tm_min,   p_tm->tm_sec,
            timeEnded.tv_usec/10000 );
        a = int(atof(time_str)*1000);

        p_time = (time_t *)&(timeBegined.tv_sec);
        p_tm = localtime(p_time);
        sprintf( time_str, "%02d%02d%02d.%03ld",
            p_tm->tm_hour,      p_tm->tm_min,   p_tm->tm_sec,
            timeBegined.tv_usec/10000 );
        a = a - int(atof(time_str)*1000);

        printf("   Elapsed Time : %.3f sec\n", float(a)/1000.0f);

    }
};



QueryPerformanceCounter

๊ทผ๋ฐ... ๋” ์ข‹์€ ๋ฐฉ์‹์ด ์žˆ๋‹ค.

void InitnStartCheckElapsedTime( void )
{
    LARGE_INTEGER qwTicksPerSec, qwTime;
    m_bUsingQPF = (bool) (QueryPerformanceFrequency( &qwTicksPerSec ) != 0);
 
    if( !m_bUsingQPF )
        return;
 
    m_llQPFTicksPerSec = qwTicksPerSec.QuadPart;
 
    QueryPerformanceCounter( &qwTime );
    m_llLastElapsedTime = qwTime.QuadPart;
}

float GetElapsedTime( void )
{
    if( !m_bUsingQPF )
        return -1.0;
 
    LARGE_INTEGER qwTime;
    QueryPerformanceCounter( &qwTime );
 
    float fElapsedTime = (float)((double) ( qwTime.QuadPart - m_llLastElapsedTime ) / (double) m_llQPFTicksPerSec);
    m_llLastElapsedTime = qwTime.QuadPart;
 
    return fElapsedTime;
}


์ถœ์ฒ˜: https://kindtis.tistory.com/79 [Game Programmer Life]

 

๋ฉ€ํ‹ฐ์ฝ”์–ด ๊ธฐ๋ฐ˜ ์‹œ์Šคํ…œ์—์„œ ์˜ค์ž‘๋™

์ถœ์ฒ˜: 2009.08.22.๋ช…๊ตญ์ง„(http://magicpotato.com)

์—ฌ๋Ÿฌ๊ฐœ์˜ CPU ๋˜๋Š” ๋ฉ€ํ‹ฐ์ฝ”์–ด๊ธฐ๋ฐ˜์˜ ์‹œ์Šคํ…œ์—์„œ timeGetTime(), QueryPerformanceCounter()๋“ฑ ๋ช‡๊ฐ€์ง€ ์‹œ๊ฐ„ํ•จ์ˆ˜๊ฐ€ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

BIOS๋‚˜ ๋“œ๋ผ์ด๋ฒ„์˜ ๋ฌธ์ œ๋กœ ์ธํ•ด์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฒ„๊ทธ์ธ๋ฐ ์ตœ๊ทผ์—๋Š” ๊ฑฐ์˜ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ œ์ง€๋งŒ, ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋ผ๋ฉด ๋‹น์—ฐํžˆ ์•Œ์•„๋‘ฌ์•ผ ํ•˜๋Š” ์ด์Šˆ์ด๋‹ค.

์‹œ๊ฐ„์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์“ฐ๋ ˆ๋“œ๋Š” SetThreadAffinityMask() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํ•œ๊ฐœ์˜ CPU์—์„œ๋งŒ ์ž‘๋™ํ•˜๋„๋ก ๊ฐ•์ œ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

ํ•ด๋‹น ๋ฌธ์ œ์— ๋Œ€ํ•œ ๋ฌธ์„œ๋Š” ๋‹ค์Œ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
MSDN Technical Articles - Game Timing and Multicore Processors
http://msdn.microsoft.com/en-us/library/ee417693(VS.85).aspx

728x90
๋ฐ˜์‘ํ˜•