High-precision timing method and device for real-time simulation of Windows system, storage medium and electronic equipment
By comparing the processor clock time with the simulation time in Windows system, and performing the time calibration algorithm cycles, combining the coarse timing function and processor clock time query, the problem that the timer function in the existing technology cannot guarantee high precision is solved, and the High precision timing and reduce CPU resource usage.
Patent Information
- Application Number
- CN202411353430.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-26
- Publication Date
- 2025-02-18
- Estimated Expiration
- 2044-09-26
AI Technical Summary
The timer function of existing Windows systems cannot guarantee high-precision time control. It is affected by operating system scheduling and system load, resulting in a deviation from the actual execution time of simulation tasks.
By comparing the processor clock time with the simulation time, performing a preset time calibration algorithm cycle until the processor clock time is greater than or equal to the simulation time, combining the coarse timing function and query of the processor clock time, high-precision timing is achieved.
While ensuring high precision, it reduces the use of CPU resources and improves the overall performance and reliability of the system.
Smart Images

Figure CN119473492A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a high-precision timing method, device, storage medium and electronic device for real-time simulation of Windows system. Background Art
[0002] In the field of real-time simulation of digital models of physical systems, each step of the simulation process needs to be precisely timed. In the related art, dedicated real-time simulation computers can be used for simulation, but the cost is high and the development time of the simulation program is long.
[0003] Therefore, in order to reduce costs, ordinary computers can also be used for simulation calculations. However, the timer functions provided by the operating system on ordinary computers usually cannot guarantee high-precision time control. The triggering of these functions may be affected by the operating system scheduling and system load, resulting in a deviation between the actual execution time of the scheduled task and the expected time.
[0004] Based on this, there is an urgent need for a high-precision timing method for real-time simulation of Windows systems to improve the timing accuracy of the operating system. Summary of the invention
[0005] The purpose of this application is to provide a high-precision timing method, device, storage medium and electronic device for real-time simulation of Windows system, which are used to improve the timing accuracy of the operating system.
[0006] The present application provides a high-precision timing method for real-time simulation of Windows system, comprising: Compare the first time corresponding to the current moment with the second time corresponding to the current moment, and generate a first comparison result; the first time is: the processor clock time calculated based on the initial value of the high-resolution performance counter; the second time is: calculated based on the simulation time at the current moment and the sum of the timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous round of simulation; when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, loop execute the preset time calibration algorithm until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; execute the target simulation function.
[0007] Optionally, the first time corresponding to the current moment is calculated based on the following steps: obtaining a current value of the high-resolution performance counter, and obtaining the current time based on the current value; calculating a target difference between the current time and an initial time, and determining a ratio of the target difference to the frequency of the high-resolution performance counter as the first time corresponding to the current moment; The initial time is: the time corresponding to the initial value of the high-resolution performance counter.
[0008] Optionally, the loop executes a preset time calibration algorithm until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment, including: calculating the target time difference between the first time and the simulation time of the current moment, and performing a corresponding target operation based on a second comparison result of the target time difference and the fine timing time, until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; wherein the target operation includes: a first operation and a second operation; the first operation is: when the target time difference is greater than the fine timing time, using a coarse timing function to sleep for a first preset time; the second operation is: when the target time difference is less than or equal to the fine timing time, sleeping for a second preset time by repeatedly querying the processor clock time.
[0009] Optionally, executing a corresponding target operation based on a second comparison result between the target time difference and the fine timing time includes: determining the size of the target time difference and the fine timing time, and generating a second comparison result; if the second comparison result indicates that the target time difference is greater than the fine timing time, executing the first operation, otherwise, executing the second operation.
[0010] Optionally, executing the first operation includes: creating a target event; calling a coarse timing function of the operating system to set a target timer, triggering the target event after the first preset time period; and ending the target event when the time object of the target event changes to a signal state.
[0011] Optionally, executing the second operation includes: querying a processor clock time of the operating system at a current moment.
[0012] Optionally, after comparing the first time corresponding to the current moment with the second time corresponding to the current moment and generating a first comparison result, the method further includes: executing the target simulation function when the first comparison result indicates that the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment.
[0013] The present application also provides a high-precision timing device for real-time simulation of Windows system, comprising: A comparison module is used to compare a first time corresponding to a current moment with a second time corresponding to the current moment, and generate a first comparison result; the first time is: a processor clock time calculated based on an initial value of a high-resolution performance counter; the second time is: calculated based on the simulation time at the current moment and the sum of a timing precision control amount; the simulation time at the current moment is calculated based on the sum of a simulation time and a simulation step size of a previous round of simulation; an execution module is used to cyclically execute a preset time calibration algorithm when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; the execution module is also used to execute a target simulation function.
[0014] Optionally, the device also includes: an acquisition module and a calculation module; the acquisition module is used to acquire the current value of the high-resolution performance counter and obtain the current time based on the current value; the calculation module is used to calculate the target difference between the current time and the initial time, and determine the ratio of the target difference to the frequency of the high-resolution performance counter as the first time corresponding to the current moment; wherein the initial time is: the time corresponding to the initial value of the high-resolution performance counter.
[0015] Optionally, the calculation module is also used to calculate the target time difference between the first time and the simulation time of the current moment; the execution module is specifically used to execute the corresponding target operation based on the second comparison result of the target time difference and the fine timing time, until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; wherein the target operation includes: a first operation and a second operation; the first operation is: when the target time difference is greater than the fine timing time, using a coarse timing function to sleep for a first preset time; the second operation is: when the target time difference is less than or equal to the fine timing time, sleeping for a second preset time by repeatedly querying the processor clock time.
[0016] Optionally, the comparison module is also used to determine the size of the target time difference and the fine timing time, and generate a second comparison result; the execution module is specifically used to execute the first operation if the second comparison result indicates that the target time difference is greater than the fine timing time, otherwise, execute the second operation.
[0017] Optionally, the execution module is specifically used to create a target event; the execution module is also specifically used to call the coarse timing function of the operating system to set a target timer, triggering the target event after the first preset time period; the execution module is also specifically used to end the target event when the time object of the target event becomes a signal state.
[0018] Optionally, the execution module is specifically used to query the processor clock time of the operating system at a current moment.
[0019] Optionally, the execution module is further used to execute the target simulation function when the first comparison result indicates that a first time corresponding to the current moment is greater than or equal to a second time corresponding to the current moment.
[0020] The present application also provides a computer program product, including a computer program / instruction, which, when executed by a processor, implements the steps of any of the above-mentioned high-precision timing methods for real-time simulation of Windows systems.
[0021] The present application also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps of the high-precision timing method for real-time simulation of a Windows system as described in any one of the above are implemented.
[0022] The present application also provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the steps of the high-precision timing method for real-time simulation of a Windows system as described in any one of the above are implemented.
[0023] The high-precision timing method, device, storage medium and electronic device for real-time simulation of Windows system provided by the present application, first, compare the first time corresponding to the current moment with the second time corresponding to the current moment, and generate a first comparison result; the first time is: the processor clock time calculated based on the initial value of the high-resolution performance counter; the second time is: calculated based on the simulation time at the current moment and the sum of the timing precision control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step length of the previous round of simulation; then, when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, the preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; finally, the target simulation function is executed. In this way, while ensuring high precision, the occupation of CPU resources can be reduced, thereby improving the overall performance and reliability of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] In order to more clearly illustrate the technical solutions in the present application or the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0025] Figure 1 It is one of the flow charts of the high-precision timing method for real-time simulation of Windows system provided by this application; Figure 2 This is the second flow chart of the high-precision timing method for real-time simulation of Windows system provided by this application; Figure 3 It is a structural schematic diagram of a high-precision timing device for real-time simulation of Windows system provided by the present application; Figure 4 It is a structural schematic diagram of the electronic device provided by this application. DETAILED DESCRIPTION
[0026] In order to make the purpose, technical solutions and advantages of this application clearer, the technical solutions in this application will be clearly and completely described below in conjunction with the drawings in this application. Obviously, the described embodiments are part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0027] The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the present application can be implemented in an order other than those illustrated or described here, and the objects distinguished by "first", "second", etc. are generally of one type, and the number of objects is not limited. For example, the first object can be one or more. In addition, "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally indicates that the objects associated with each other are in an "or" relationship.
[0028] Taking Windows system as an example, the existing timer functions under Windows operating system, such as setTimer, createWaitableTimer and timeSetEvent, usually cannot guarantee high-precision time control. The triggering of these functions may be affected by the operating system scheduling and system load, resulting in a deviation between the actual execution time of the scheduled task and the expected time. Although the QueryPerformanceCounter() function can provide an accurate hardware clock, it may occupy the central processing unit CPU resources for a long time during its use, resulting in excessive system load. In view of the accuracy and CPU load problems in the prior art, an improved time processing mechanism is urgently needed. This mechanism should be able to reduce the occupation of CPU resources while ensuring high accuracy, thereby improving the overall performance and reliability of the system.
[0029] In view of the above technical problems existing in the related art, the embodiment of the present application proposes a high-precision timing method for real-time simulation of Windows system, aiming to solve the above problems of the existing timer function and time detection application program interface API. The method includes combining the timer function and the time detection API to obtain the hardware clock while minimizing the occupation of CPU resources.
[0030] The following, in conjunction with the accompanying drawings, describes in detail the high-precision timing method for real-time simulation of Windows systems provided by the embodiments of the present application through specific embodiments and their application scenarios.
[0031] like Figure 1 As shown, an embodiment of the present application provides a high-precision timing method for real-time simulation of a Windows system, and the method may include the following steps 101 to 103: Step 101: compare a first time corresponding to the current moment with a second time corresponding to the current moment, and generate a first comparison result.
[0032] Among them, the first time is: the processor clock time calculated based on the initial value of the high-resolution performance counter; the second time is: calculated based on the simulation time at the current moment and the sum of the timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous round of simulation.
[0033] Exemplarily, the first time is the processor clock time at the current moment, and the second time is the sum of the simulation time at the current moment and the timing precision control amount. It is understandable that the embodiment of the present application includes two timelines, namely the processing clock time and the simulation time. When the processor clock time is significantly less than the simulation time, it is necessary to control the processing clock time to catch up with the simulation time by means of sleep, timing, etc., that is, when the first time is less than the second time, the first time is made greater than or equal to the second time by means of sleep, timing, etc.
[0034] Exemplarily, in an embodiment of the present application, when simulation is performed, in the simulation function startup part, the high-resolution timestamp of the query processor is used as the initial time reference; in the part that needs to be timed in each step of the simulation, the current simulation time is calculated based on the simulation times counter and the simulation time step.
[0035] Specifically, the step of calculating the first time in the above step 101 may include the following steps 101a1 and 101a2: Step 101a1: Obtain the current value of the high-resolution performance counter, and obtain the current time based on the current value.
[0036] Step 101a2: Calculate a target difference between the current time and the initial time, and determine a ratio of the target difference to the frequency of the high-resolution performance counter as a first time corresponding to the current moment.
[0037] The initial time is: the time corresponding to the initial value of the high-resolution performance counter.
[0038] For example, in the embodiment of the present application, the QueryPerformanceCounter function can be used to obtain the current high-resolution timestamps, and these timestamps are converted into processor clock time dClockTime, the code is as follows: QueryPerformanceCounter(&liInstantTimeCount); timeCount = liInstantTimeCount.QuadPart -liInitialTimeCount.QuadPart; dClockTime = timeCount / dClockFrequency.
[0039] Among them, QueryPerformanceCounter(&liInstantTimeCount), this function obtains the current value of the high-resolution performance counter. The result is stored in the liInstantTimeCount structure.
[0040] timeCount = liInstantTimeCount.QuadPart - liInitialTimeCount.QuadPart. The difference between the current time (liInstantTimeCount.QuadPart) and the initial time (liInitialTimeCount.QuadPart) gives the elapsed time in count units. liInitialTimeCount is a high-resolution timestamp of the initial moment obtained using the QueryPerformanceCounter function.
[0041] dClockTime = timeCount / dClockFrequency. To get the elapsed time in seconds, divide the count value (timeCount) by the frequency of the performance counter (dClockFrequency).
[0042] It should be noted that in the embodiment of the present application, the processor clock time at the current moment, that is, the first time mentioned above, needs to be calculated multiple times, and each calculation is performed according to the calculation method provided by the above code.
[0043] Step 102: When the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, a preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment.
[0044] Exemplarily, in the embodiment of the present application, by comparing the processor clock time dClockTime and the simulation time dSimTime, different timing functions can be used according to the comparison result to achieve high-precision timing.
[0045] Specifically, the above step 102 may further include the following step 102a: Step 102a, calculate the target time difference between the first time and the simulation time of the current moment, and perform the corresponding target operation based on the second comparison result of the target time difference and the fine timing time, until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment.
[0046] Among them, the target operation includes: a first operation and a second operation; the first operation is: when the target time difference is greater than the fine timing time, using a coarse timing function to sleep for a first preset time; the second operation is: when the target time difference is less than or equal to the fine timing time, sleeping for a second preset time by repeatedly querying the processor clock time.
[0047] It is understandable that, when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, different operations can be performed according to the target time difference between the first time and the simulation time of the current moment. For example, when the time difference is large, that is, the target time difference is greater than the fine timing time, the rough timing function of the operating system can be called to put the simulation process to sleep; when the time difference is small, that is, the target time difference is less than or equal to the fine timing time, the timing function of the operating system of the operating system can be called to put the simulation process to sleep.
[0048] It should be noted that the above rough timing function may include: setTimer, createWaitableTimer, timeSetEvent, etc. The above timing function of the Windows operating system may include: QueryPerformanceCounter function.
[0049] Specifically, in the above step 102a, the step of performing the corresponding target operation based on the second comparison result between the target time difference and the fine timing time may further include the following steps 102a1 and 102a2: Step 102a1, determine the size of the target time difference and the fine timing time, and generate a second comparison result.
[0050] Step 102a2: If the second comparison result indicates that the target time difference is greater than the fine timing time, then the first operation is performed; otherwise, the second operation is performed.
[0051] Exemplarily, at each step of the simulation, a loop is performed to check whether the current processor clock time is less than the current simulation time minus the fine timing time. If so, the coarse timing function sleep loop continues; if not, the current processor clock time is queried in a loop.
[0052] It should be noted that the above steps 102a1 and 102a2 are steps that are executed cyclically, that is, each time the first operation or the second operation is executed, the above steps 102a1 and 102a2 need to be executed cyclically, and after the above step 102a2 is executed, the following step 102a3 needs to be executed: Step 102a3: compare the first time corresponding to the current moment with the second time corresponding to the current moment, and generate a third comparison result.
[0053] It is understandable that after executing the above step 102a2, it is necessary to determine whether the first time is greater than or equal to the second time, and when the above third comparison result indicates that the first time is greater than or equal to the second time, execute the code corresponding to the target simulation function.
[0054] Specifically, in the above step 102, the step of performing the first operation may further include the following steps 102b1 to 102b3: Step 102b1: Create a target event.
[0055] Step 102b2: Call the rough timing function of the operating system to set a target timer, and trigger the target event after the first preset time period.
[0056] Step 102b3: When the time object of the target event changes to a signal state, the target event is ended.
[0057] For example, in order to maintain the continuity and accuracy of the simulation, it is necessary to continuously call the Windows timer function in the simulation loop. In the simulation main loop, the timer function is continuously called to reset the timer to ensure the continuity of the simulation. Due to the low accuracy of the timer function timing, this patent only uses the timer function timing for rough timing. Rough timing sets a one-time timing event by creating an event object, setting the timer and waiting for the time to be set to the signal state. The specific code is as follows: HANDLE hEvent = CreateEvent(NULL,FALSE,FALSE,NULL); if (!hEvent)
[0058] {
[0059] printf("CreateEvent failed (%d)\n", GetLastError()); }
[0060] if (!timeSetEvent(dwMilliseconds,1,(LPTIMECALLBACK)hEvent,0,TIME_ONESHOT | TIME_CALLBACK_EVENT_SET))
[0061] {
[0062] printf("timeSetEvent failed (%d)\n", GetLastError()); }
[0063] if (WaitForSingleObject(hEvent,INFINITE) != WAIT_OBJECT_0)
[0064] {
[0065] printf("WaitForSingleObject failed (%d)\n", GetLastError()); }
[0066] CloseHandle(hEvent).
[0067] First, an event object hEvent is created, which is a synchronization object used for communication between different threads. Then, the timeSetEvent function is called to set a timer. This function triggers a callback function (hEvent in this case) after a specified time interval.
[0068] The dwMilliseconds parameter indicates the time interval of the timer in milliseconds. The TIME_ONESHOT flag indicates that this is a one-time timer event, not a periodic one. The TIME_CALLBACK_EVENT_SET flag indicates that when the timer is triggered, the hEvent event object will be set to the signaled state. Finally, wait for the hEvent event object to become signaled and then close the event object.
[0069] Specifically, in the above step 102, the step of performing the second operation may further include the following step 102c: Step 102c: query the processor clock time of the operating system at the current moment.
[0070] Exemplarily, in order to ensure high precision of the simulation, it is necessary to query the current processor clock time for precise timing in each simulation step and compare it with the simulation time to determine whether it is necessary to sleep or continue the simulation.
[0071] Step 103: Execute the target simulation function.
[0072] Exemplarily, after the simulation process is put to sleep through the above-mentioned first operation and second operation, so that the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment, the code corresponding to the simulation function can be executed. Through the high-precision timing method in the above-mentioned embodiment, high-precision simulation timing can be achieved under the operating system while maintaining efficient use of CPU resources.
[0073] Exemplarily, after the above step 101, the high-precision timing method for real-time simulation of Windows system provided in the embodiment of the present application may further include the following step 104: Step 104: When the first comparison result indicates that the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment, execute the target simulation function.
[0074] For example, Figure 2 As shown, it is a detailed flow chart of the high-precision timing method for real-time simulation of Windows system provided by the embodiment of the present application. Step 1, calculate the initial time base of the processor, and calculate the processor clock time at any moment based on the base. Step 2, calculate the simulation time at the current moment, and the current simulation time is calculated after the simulation time and the simulation step length of the previous round. Step 3, determine whether the processor clock time at the current moment is greater than or equal to the sum of the simulation time at the current moment and the timing precision control amount. If so, jump directly to step 5, otherwise, continue to execute step 4. Step 4, determine whether the processor clock time at the current moment is less than or equal to the difference between the simulation time at the current moment and the fine timing time. If so, call the operating system timer function (i.e. the above-mentioned rough timing function) cyclically, otherwise, query the processor clock time at the current moment cyclically until the processor clock time at the current moment is greater than or equal to the sum of the simulation time at the current moment and the timing precision control amount. Step 5, call the simulation-related function code.
[0075] In this way, by combining timer function timing and processor cycle query time, we can not only achieve high-precision time control of the simulation process and ensure the accuracy and reliability of the simulation, but also effectively reduce the long-term occupation of CPU resources and improve the overall performance of the system.
[0076] The high-precision timing method for real-time simulation of Windows system provided in the embodiment of the present application, first, compare the first time corresponding to the current moment with the second time corresponding to the current moment, and generate a first comparison result; the first time is: the processor clock time calculated based on the initial value of the high-resolution performance counter; the second time is: calculated based on the simulation time at the current moment and the sum of the timing precision control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step length of the previous round of simulation; then, when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, the preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; finally, the target simulation function is executed. In this way, the occupation of CPU resources can be reduced while ensuring high precision, thereby improving the overall performance and reliability of the system.
[0077] It should be noted that the high-precision timing method for real-time simulation of Windows system provided in the embodiment of the present application can be executed by a high-precision timing device for real-time simulation of Windows system, or a control module for executing the high-precision timing method for real-time simulation of Windows system in the high-precision timing device for real-time simulation of Windows system. In the embodiment of the present application, the high-precision timing device for real-time simulation of Windows system executing the high-precision timing method for real-time simulation of Windows system is taken as an example to illustrate the high-precision timing device for real-time simulation of Windows system provided in the embodiment of the present application.
[0078] It should be noted that in the embodiments of the present application, the high-precision timing method for real-time simulation of Windows system shown in the above-mentioned method drawings is illustrated by combining an accompanying drawing in the embodiments of the present application as an example. In specific implementation, the high-precision timing method for real-time simulation of Windows system shown in the above-mentioned method drawings can also be implemented in combination with any other drawings that can be combined as shown in the above-mentioned embodiments, which will not be repeated here.
[0079] The high-precision timing device for real-time simulation of Windows system provided by the present application is described below. The high-precision timing method for real-time simulation of Windows system described above and described below can be referenced to each other.
[0080] Figure 3 A schematic diagram of the structure of a high-precision timing device for real-time simulation of Windows system provided in an embodiment of the present application, such as Figure 3 As shown, specifically including: A comparison module 301 is used to compare a first time corresponding to a current moment with a second time corresponding to the current moment, and generate a first comparison result; the first time is: a processor clock time calculated based on an initial value of a high-resolution performance counter; the second time is: calculated based on the simulation time at the current moment and the sum of a timing precision control amount; the simulation time at the current moment is calculated based on the sum of a simulation time and a simulation step size of a previous round of simulation; an execution module 302 is used to, when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, cyclically execute a preset time calibration algorithm until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; the execution module 302 is also used to execute a target simulation function.
[0081] Optionally, the device also includes: an acquisition module and a calculation module; the acquisition module is used to acquire the current value of the high-resolution performance counter and obtain the current time based on the current value; the calculation module is used to calculate the target difference between the current time and the initial time, and determine the ratio of the target difference to the frequency of the high-resolution performance counter as the first time corresponding to the current moment; wherein the initial time is: the time corresponding to the initial value of the high-resolution performance counter.
[0082] Optionally, the calculation module is also used to calculate the target time difference between the first time and the simulation time of the current moment; the execution module 302 is specifically used to execute the corresponding target operation based on the second comparison result of the target time difference and the fine timing time, until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; wherein the target operation includes: a first operation and a second operation; the first operation is: when the target time difference is greater than the fine timing time, using a coarse timing function to sleep for a first preset time; the second operation is: when the target time difference is less than or equal to the fine timing time, sleeping for a second preset time by repeatedly querying the processor clock time.
[0083] Optionally, the comparison module 301 is also used to determine the size of the target time difference and the fine timing time, and generate a second comparison result; the execution module 302 is specifically used to execute the first operation if the second comparison result indicates that the target time difference is greater than the fine timing time, otherwise, execute the second operation.
[0084] Optionally, the execution module 302 is specifically used to create a target event; the execution module 302 is also specifically used to call the coarse timing function of the operating system to set a target timer, triggering the target event after the first preset time period; the execution module 302 is also specifically used to end the target event when the time object of the target event becomes a signal state.
[0085] Optionally, the execution module 302 is specifically used to query the processor clock time of the operating system at a current moment.
[0086] Optionally, the execution module 302 is further configured to execute the target simulation function when the first comparison result indicates that a first time corresponding to the current moment is greater than or equal to a second time corresponding to the current moment.
[0087] The high-precision timing device for real-time simulation of Windows system provided by the present application first compares the first time corresponding to the current moment with the second time corresponding to the current moment, and generates a first comparison result; the first time is: the processor clock time calculated based on the initial value of the high-resolution performance counter; the second time is: calculated based on the sum of the simulation time at the current moment and the timing precision control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step length of the previous round of simulation; then, when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, the preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; finally, the target simulation function is executed. In this way, the occupation of CPU resources can be reduced while ensuring high precision, thereby improving the overall performance and reliability of the system.
[0088] Figure 4 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 4As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute a high-precision timing method for real-time simulation of a Windows system, the method comprising: comparing a first time corresponding to the current moment with a second time corresponding to the current moment, and generating a first comparison result; the first time is: a processor clock time calculated based on an initial value of a high-resolution performance counter; the second time is: calculated based on the sum of the simulation time at the current moment and the timing precision control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step length of the previous round of simulation; when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, a preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; and the target simulation function is executed.
[0089] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium, including several instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.
[0090] On the other hand, the present application also provides a computer program product, which includes a computer program stored on a computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer can execute the high-precision timing method for real-time simulation of a Windows system provided by the above-mentioned methods, the method including: comparing a first time corresponding to a current moment with a second time corresponding to the current moment, and generating a first comparison result; the first time is: a processor clock time calculated based on an initial value of a high-resolution performance counter; the second time is: calculated based on the simulation time at the current moment and the sum of a timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of a simulation time and a simulation step size of a previous round of simulation; when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, a preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; and the target simulation function is executed.
[0091] On the other hand, the present application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the above-mentioned high-precision timing method for real-time simulation of Windows system, the method comprising: comparing a first time corresponding to a current moment with a second time corresponding to the current moment, and generating a first comparison result; the first time is: a processor clock time calculated based on an initial value of a high-resolution performance counter; the second time is: calculated based on the simulation time at the current moment and the sum of a timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of a simulation time and a simulation step size of a previous round of simulation; when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, a preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; and the target simulation function is executed.
[0092] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0093] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0094] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A high-precision timing method for real-time simulation of Windows system, characterized in that: include: Compare a first time corresponding to the current moment with a second time corresponding to the current moment, and generate a first comparison result; The first time is: a processor clock time calculated based on an initial value of a high-resolution performance counter; The second time is calculated based on the sum of the simulation time at the current moment and the timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step length of the previous round of simulation; When the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, the preset time calibration algorithm is executed repeatedly until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; Executes target simulation functions.
2. The method according to claim 1, characterized in that The first time corresponding to the current moment is calculated based on the following steps: Obtaining a current value of the high-resolution performance counter, and obtaining a current time based on the current value; Calculating a target difference between the current time and an initial time, and determining a ratio of the target difference to the frequency of the high-resolution performance counter as a first time corresponding to the current moment; The initial time is: the time corresponding to the initial value of the high-resolution performance counter.
3. The method according to claim 1 or 2, characterized in that: The loop executes the preset time calibration algorithm until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment, including: Calculating a target time difference between the first time and the simulation time at the current moment, and performing a corresponding target operation based on a second comparison result between the target time difference and the fine timing time, until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; Among them, the target operation includes: a first operation and a second operation; the first operation is: when the target time difference is greater than the fine timing time, using a coarse timing function to sleep for a first preset time; the second operation is: when the target time difference is less than or equal to the fine timing time, sleeping for a second preset time by repeatedly querying the processor clock time.
4. The method according to claim 3, characterized in that: The performing a corresponding target operation based on a second comparison result between the target time difference and the fine timing time comprises: Determine the size of the target time difference and the fine timing time, and generate a second comparison result; If the second comparison result indicates that the target time difference is greater than the fine timing time, the first operation is performed; otherwise, the second operation is performed.
5. The method according to claim 4, characterized in that The performing of the first operation includes: Create target event; Calling a rough timing function of the operating system to set a target timer, and triggering the target event after the first preset time period; When the time object of the target event changes to the signal state, the target event is ended.
6. The method according to claim 4, characterized in that The performing of the second operation includes: Query the operating system's current processor clock time.
7. The method according to claim 1, characterized in that After comparing the first time corresponding to the current moment with the second time corresponding to the current moment and generating a first comparison result, the method further includes: When the first comparison result indicates that the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment, the target simulation function is executed.
8. A high-precision timing device for real-time simulation of Windows system, characterized in that: The device comprises: A comparison module, used for comparing a first time corresponding to the current moment with a second time corresponding to the current moment, and generating a first comparison result; the first time is: a processor clock time calculated based on an initial value of a high-resolution performance counter; the second time is: calculated based on the sum of the simulation time at the current moment and a timing precision control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step length of the previous round of simulation; an execution module, configured to, when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, cyclically execute a preset time calibration algorithm until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; The execution module is also used to execute the target simulation function.
9. An electronic device, characterized in that: The invention comprises a memory, a processor and a computer program stored in the memory and executable on the processor. When the processor executes the program, the steps of the high-precision timing method for real-time simulation of Windows system as claimed in any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and when the computer program is executed by a processor, the steps of the high-precision timing method for real-time simulation of Windows system as claimed in any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Simulation system clock synchronization method, device and apparatus and storage medium
CN111290477A
Real-time simulation timing method and system based on multi-core processor platform and medium
CN112463328A
Step size synchronous calibration method and device in electric power real-time simulation and related equipment
CN115459897A
Simulation apparatus, simulation control method, and computer product
US20090319240A1
Simulation Method in Quantum Control, Classical Computer, and Storage Medium
US20220044143A1