29-07-2009, 13:29
|
|
|
חבר מתאריך: 30.07.05
הודעות: 949
|
|
אני חושש שעלולה להיות לך בעיה בנושא....
לפני תקופה לא גדולה הייתי צריך משהו דומה, ועשיתי מחקר קטן באינטרנט בנושא.
כעקרון, חלונות ולינוקס אינן מערכות הפעלה (RTOS (Real Time Operating System כלומר הן אינן תומכות בפעולות זמן אמת. לכן באופן עקרונית, חלון הזמן המינימאלי שאפשר לקבל ב-windows (זו הפלטפורמה עליה בדקתי בעיקר) הוא משהו כמו 10-13 מילי שניות - שזהו בערך גודל ה-time slice שנותן ה-scheduler של מערכת ההפעלה. בנוסף, בגלל שאין לך שליטה על מצב בו מערכת ההפעלה יכולה להפקיע את המעבד מהתוכנה שלך, אתה לא יכול למדוד תזמונים קטנים כ"כ בצורה מדוייקת - תחשוב שמדדת 5 מילי שניות ואז מערכת ההפעלה פשוט עושה context-switch ושמה תהליך אחר לרוץ והתהליך שלך יחזור רק עוד איזה 100-200 מילי שניות למעבד... זה בעייתי.
בזמנו לא מצאתי לזה פתרון, חוץ ממישהו שהציע לי לכתוב דרייבר... למזלי אחרי שדיברתי עם מנחה הפרויקט שלנו סגרנו על תזמונים ברזולוציה של 10 מילי שניות.
יש דרך למדוד תזמונים בצורה מדוייקת למדי שמצאתי תחת windows:
קוד PHP:
//StopWatch.cpp: #include "stdafx.h" #include "StopWatch.h"
StopWatch::StopWatch() : is_running(false) { Reset(); QueryPerformanceFrequency( &f ); } void StopWatch::Start() { if (is_running) return; // If stopwatch running already, do nothing... is_running = true; QueryPerformanceCounter(&start); }
void StopWatch::Stop() { if (!is_running) return; // If stopwatch no running, do nothing is_running = false; QueryPerformanceCounter(&end); }
void StopWatch::Reset() { is_running = false; start.QuadPart = end.QuadPart = 0; }
double StopWatch::getTimePassed() { if (is_running) QueryPerformanceCounter(&end); // If running, update with current time return ((double)(end.QuadPart - start.QuadPart)) / (double)f.QuadPart; }
קוד PHP:
//StopWatch.h #ifndef STOPWATCH_H #define STOPWATCH_H
/* ---------------------------------------------------------------------------- * File name: StopWatch.h * Implements: This class implements a stopwatch which mesures the time[sec] * passed between it's starting and stopping. * Created: May 5th, 2009. * Version: 1.0 * Note: This class will only work on MS™ Windows©. * ---------------------------------------------------------------------------- * Operations: * Start: The stopwatch will begin mesuring time from the moment this * method is called. If already running this method does nothing. * Stop: Stops the time mesurement and remmembers the time elapesed. * If not running, this method does nothing. * Reset: Resets the stopwatch. (nulls the mesurements and stops it). * getTimePassed: Calculates and returns the time passed from the call * to Start. if stoped, will return the mesured time * until Stop was called. * */
#include <windows.h>
class StopWatch { public: StopWatch(); void Start(); void Stop(); void Reset(); double getTimePassed(); private: typedef LARGE_INTEGER ticks; typedef LARGE_INTEGER frequency; ticks start, end; frequency f; bool is_running; };
#endif
אבל לתזמן ברזולוציות כאלו - זה כנראה לא אפשרי תחת windows, או לפחות לא באופן טריביאלי כלשהו. ייתכן שיש איזה API שיתן לך משהו כזה, אבל זה בעייתי...
_____________________________________
חתימתכם הוסרה כיוון שלא עמדה בחוקי האתר. לפרטים נוספים לחצו כאן. תוכלו לקבל עזרה להתאמת החתימה לחוקים בפורום חתימות וצלמיות.
|