Obliczenia wysokiej wydajności
Transkrypt
Obliczenia wysokiej wydajności
Obliczenia wysokiej wydajności Laboratorium 1 Pomiary czasu Michał Bereta [email protected] http://torus.uck.pk.edu.pl/~beretam Pomiar czasu z wykorzystaniem funkcji języka C. Gdzie szukać: MSDN - Developement Tools and Languages – Visual Studion – VisualC++ - Reference – Libraries Reference - Run-Time Library Reference – Run-Time Routines by Category – Time management Sprawdzić funkcje: clock() time() difftime() ftime() Wykorzystac typy: clock_c time_t time_b Wykorzystać zdefiniowaną stałą: CLOCKS_PER_SEC Odszukać odpowiednie pliki nagłówkowe i dołączyć do projektu. Z jaką dokładnością można zmierzyć upływ czasu za pomocą każdej z metod? Pomiar czasu z wykorzystaniem funkcji języka C z Windows API. Gdzie szukać: MSDN – Win32 and COM Development – Development Guides – Windows API – Overview of the Windows API - System Services - Time MSDN – Win32 and COM Development – Development Guides – Windows API – Windows API Reference – Functions by Category – Time Funkcje: void GetSystemTime( LPSYSTEMTIME lpSystemTime ); typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; } SYSTEMTIME, *PSYSTEMTIME; Note that the system can periodically refresh the time by synchronizing with a time source. Because the system time can be adjusted either forward or backward, do not compare system time readings to determine elapsed time. Instead, use one of the methods described in Windows Time. Windows Time Windows time is the number of milliseconds elapsed since the system started running. Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days. DWORD GetTickCount(void); The return value is the number of milliseconds that have elapsed since the system was started. The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days. To avoid this problem, use GetTickCount64. Otherwise, check for the overflow condition when comparing times. For more information, see the example below. ULONGLONG WINAPI GetTickCount64(void); UWAGA!: TYLKO W WINDOWS VISTA LUB WINDOWS SERVER LONGHORN!! High-Resolution Timer A counter is a general term used in programming to refer to an incrementing variable. Some systems include a high-resolution performance counter that provides high-resolution elapsed times. If a high-resolution performance counter exists on the system, you can use the QueryPerformanceFrequency function to express the frequency, in counts per second. The value of the count is processor dependent. On some processors, for example, the count might be the cycle rate of the processor clock. The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter. By calling this function at the beginning and end of a section of code, an application essentially uses the counter as a high-resolution timer. For example, suppose that QueryPerformanceFrequency indicates that the frequency of the high-resolution performance counter is 50,000 counts per second. If the application calls QueryPerformanceCounter immediately before and immediately after the section of code to be timed, the counter values might be 1500 counts and 3500 counts, respectively. These values would indicate that .04 seconds (2000 counts) elapsed while the code executed. The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists. The frequency cannot change while the system is running. BOOL QueryPerformanceFrequency( LARGE_INTEGER *lpFrequency ); Parameters lpFrequency [out] Pointer to a variable that receives the current performance-counter frequency, in counts per second. If the installed hardware does not support a high-resolution performance counter, this parameter can be zero. Return Value If the installed hardware supports a high-resolution performance counter, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. For example, if the installed hardware does not support a high-resolution performance counter, the function fails. BOOL QueryPerformanceCounter( LARGE_INTEGER *lpPerformanceCount ); The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter. Parameters lpPerformanceCount [out] Pointer to a variable that receives the current performance-counter value, in counts. Return Value If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. UWAGA!! LARGE_INTEGER jest unią !!! typedef union _LARGE_INTEGER { struct { DWORD LowPart; LONG HighPart; }; struct { DWORD LowPart; LONG HighPart; } u; LONGLONG QuadPart; } LARGE_INTEGER, *PLARGE_INTEGER; Pomiar czasu z wykorzystaniem funkcji języka C++ i .NET Framework. MSDN - .NET Development - .NET Framework SDK – Class Library Reference – System – DateTime structure: System::DateTime time1; time1.Ticks; The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001. System::TimeSpan diff; diff.TotalSeconds; diff.TotalMilliseconds;