1. 首页
  2. 易语言 - EasyLanguage
  3. ELTicToc:EasyLanguage微秒计时器ELTicToc, A Microsecond Timer for EasyLanguage

ELTicToc:EasyLanguage微秒计时器ELTicToc, A Microsecond Timer for EasyLanguage

本文介绍两个用于测量 EasyLanguage 代码速度的函数的用法: ELTic 和 ELToc

ELTic and ELToc are functions that you can use to measure the speed of your EasyLanguage code. ELTic( “TimerName” ) is used to create and start a timer with a user-selected name. ELToc( “TimerName” ) is used to return the number of microseconds (millionths of a second) that have elapsed since ELTic was called for the named timer. ELTic can be thought of as creating and starting a timer with the name of your choice. When ELToc is called, the named timer’s start time is subtracted from the current time and the difference between the two, in microseconds, is returned.

Thus, measuring how long it takes to run a certain block of EasyLanguage code is a two step process. First, when it is desired to “start the timer”, ELTic is called with a timer name of your choice. Then, when it is desired to “stop the timer”, ELToc is called with the same timer name as a parameter. More than one timer may be created and used in a given piece of code.

Here’s a TradeStation-compatible DLL that provides microsecond Tic and Toc functions.

Additionally, the attached DLL code demonstrates the use of the IEasyLanguageProperties interface that is available as part of the EasyLanguage Extension Software Development Kit (SDK) (tskit.dll).

When called from EasyLanguage, the demonstration DLL creates (starts) or returns the elapsed time of user-named microsecond timers. Two DLL functions are provided:

ELTic( “TimerName” ) – Used to create and start a timer with a user selected name.

ELToc( “TimerName” ) – Used to return the number of microseconds (millionths of a second) that have elapsed since ELTic was called for the named timer.

The following files are contained in the attached zip file:

EasyLanguage Tic Toc Demo.tsw – Demonstration TradeStation workspace.

ELTicToc.dll – Compiled TradeStation-compatible DLL.

Tic Toc Demo Indicator.ELD – ELD file containing the Tic Toc Demo indicator, ready for importation into TradeStation.

ELTicToc.sln, ELTicToc.vcproj, ELTicTocDef.def, ELTicTocMain.cpp – Together, these files comprise the Visual C++ project from which the DLL is created.

Running the Demo

Running the demonstration is a five step process:

1.) Download the file EasyLanguage Tic Toc Demo.zip, posted above, to your computer.

2.) Extract the compiled DLL contained in the zip file, ELTicToc.dll, to your installation’s equivalent to the following subdirectory:

C:\WINDOWS\system32\

3.) Double-click the .ELD file, and import the indicator contained therein.

4.) Move the TradeStation workspace file, EasyLanguage Tic Toc Demo.tsw, into your \Program Files\TradeStation\MyWork\ subdirectory.

5.) Open the demonstration workspace during a time when live market data is flowing and ensure that the DLL-calculated performance times for a section of the demonstration code plot on the chart after 10 “real-time” ticks have passed.

The demonstration code waits until 10 “real-time” ticks have passed. It then uses the microsecond performance timer to measure the time it takes to run a simple EasyLanguage code loop 1,000 times. The time it takes to perform this task is plotted on the chart. (Plotted times are in microseconds (millionths of a second). To convert to seconds, divide by 1,000,000.)

How it Works

Most modern PC’s are equipped with a high-speed counter the count and frequency of which are both programmatically accessible. (This high speed counter is separate from the computer’s time-of-day clock.) This high-speed counter can be used as a timer.

The DLL provides two functions, ELTic and ELToc, that can be called from EasyLanguage.

ELTic records the current performance counter count. This count is stored using the IEasyLanguageProperties interface of the calling EasyLanguage analysis technigue. This process can be thought of as the process of “creating” a timer of a name of your choice.

ELToc determines the current performance counter value. It then subtracts the starting performance counter value for the named timer, the value established when ELTic was called for the timer, and returns the difference between the two, in microseconds.

Thus, to measure any time period is a two-step process. First, when it is desired to “start the timer”, ELTic is called with a given timer name of your choice. Then, when it is desired to “stop the timer”, ELToc is called with the same timer name as a parameter.

Multiple timers may be used in a given piece of code.

Timers may be nested, like this:

ELTic( “Timer1” ) ;

  { Code block 1… }

   ELTic( “Timer2” ) ;

      { Code block 2… }

   ELToc( “Timer2” ) ;

  { Code block 3… }

ELToc( “Timer1” ) ;

Of course, if nested timers are used, measured times will include both the time for code to run and the time required to call timer functions. For example, in the above pseudocode, Timer1 would measure not only the time it takes to run code blocks 1, 2, and 3, but also the time it takes to make the ELTic and ELToc calls for Timer2.

Measured times may not exceed 2 billion microseconds (2,000 seconds, about 33 minutes). Attempts to time events that exceed this duration will result in a return value of -1 from the call to ELToc.

A runtime error will be generated in TradeStation if the DLL is unable to determine the frequency of the PC’s performance counter.

A runtime error will be generated in TradeStation if an attempt is made to read a timer that does not exist. This would occur, for example, if ELToc were called with a specific timer name before ELTic had been called with that timer name.

Here is a practical example we were working with recently. The following code compares the time required to execute GetPositionQuantity to the time required to execute a simple addition and assignment operation (X = X + 1). Since GetPositionQuantity makes an out-of-process call to the TradeManager, we expect it to be slower than the addition operation.

external method:  "ELTicToc.dll", int, "ELTIC", LPSTR ;
external method:  "ELTicToc.dll", int, "ELTOC", LPSTR ;

Value1 = ELTic( "Timer1" ) ;
Value2 = GetPositionQuantity( "Symbol", "Account" ) ;
Plot1( ELToc( "Timer1" ), "TradeManager" ) ;

Value1 = ELTic( "Timer2" ) ;
Value3 = Value3 + 1 ;
Plot2( ELToc( "Timer2" ), "Addition" ) ;

If you run this code, substitute your own account number for “Account” and a symbol of interest for ‘Symbol’. We found the TradeManager keyword GetPositionQuantity takes about 1300 microseconds, while the simple addition operation Value3 = Value3 + 1 requires about 25 microseconds. Results like this may help you optimize the execution speed of your code.

更新于 2020年6月1日

这篇文章对您有帮助吗?

相关的话题

留言评论