TweetFollow Us on Twitter

Accurate Timing
Volume Number:10
Issue Number:4
Column Tag:Useful Tricks

Related Info: Time Manager

A Sophisticate’s Primer
on Accurate Timing

So you think your code is faster? Now you’ve got a tool to be sure.

By Bill Karsh

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

About the author

Bill is a trained experimental physicist with 13 years experience in data analysis and software development at various accelerator laboratories. After 18 months as a project leader on Mac products for PowerCore, he’s now a hopeful entrepreneur working on both a programming tools package and a desktop publishing layout utility.

Our subject is the accurate timing of two alternative versions of similarly functioning code. Is one truly faster? By how much? Is the difference significant? These are the sorts of things one might like to find out for a Programmer’s Challenge entry, or to improve the performance of time-critical operations, getting the edge over competitor products. Obtaining accurate timing information has been an ongoing project of mine for some time. What is presented here is a distillation of the best techniques I have found so far. I apologize in advance to those readers who are knowledgeable on such topics as interrupt processing and elementary statistics. Since these ideas are necessary for the understanding and proper use of the methods, I include brief discussions in deference to the novice.

Sources of Error

The general scheme for timing a function, call it Foo, is to read a clock, execute Foo, then read the clock again. The difference between the two readings ought to be the duration of Foo’s execution. This is more or less true. The single largest flaw in this simple idea is that Foo’s execution is repeatedly interrupted, while the clock continues to run. Since these interrupts occur pseudo-randomly in time, the elapsed time we measure this way fluctuates pseudo-randomly from one measurement to the next. Therefore, any better method is necessarily a statistical one.

It may be worth pointing out why there are interrupts, and why their character appears random. An interrupt is a temporary shifting of the CPU’s attention away from what it is currently doing (usually, executing application code), to a task of some immediate importance (usually an OS housekeeping chore). When the task is completed, the CPU resumes what it was doing as if nothing happened. Note that part of interrupt processing involves saving and restoring the complete machine state. What are these tasks?

Interrupts occur as a result of special conditions, collectively termed exceptions. Exceptions come in three flavors: internal, asynchronous and synchronous. Internal exceptions are mainly generated by software. These are such things as zero divide errors, explicit TRAP instructions, or calls to the Tool Box. There are many others. However, internal exceptions do not concern us since they are part of the normal execution of code.

Asynchronous exceptions are unscheduled and due to external influences. Among these are mouse movements and serial communications events like a character received or buffer empty message from a modem. The Serial Communications Controller (SCC) chip is largely responsible for catching these and telling the CPU about them. Asynchronous exceptions are of little concern, but one should not move the mouse more than necessary during a timing run.

Synchronous exceptions are scheduled housekeeping operations and occur at regular intervals. Among these are: updating Time, updating Ticks, running tasks scheduled in the VBL and Time Manager queues, and sensing stack collisions with heaps (stack sniffer). The Versatile Interface Adapter (VIA) chip is responsible for most such scheduled activities. These are of importance for us. Most of the synchronous tasks have a fixed period and take a standard amount of time to perform. Still others are scheduled with a fixed period, say every 1/60 s, but are checkup tasks. That is, they regularly check to see if something or other needs to be serviced. How long these take to run depends upon whether servicing is required and how long that servicing takes. Generally, the impact of interrupt processing time on our measurements varies according to these three factors: how many interrupts occur, how long they take and the synchronization of the interrupts with Foo (how much overlap).

If you apply the methods demonstrated here you will observe for yourself how the resulting timing errors are distributed. The overall shape is something like a bell. This is one of the most often encountered distributions for random processes. It’s called a Gaussian or Normal distribution. However, unlike a real Gaussian, which is a continuous curve, our frequencies have a discrete spectrum, coming in only a handful of sizes. Hence, the errors are pseudo-random.

Figure 1 Normal Curve

Timers

Obviously, to make timing measurements we need a suitable timer. The Mac offers several with varying levels of resolution (smallest time interval that can be measured).

Timer Resolution

Time (0x020C) Low Mem variable 1 s

Ticks (0x016A) Low Mem variable 1/60 s

VIA timer 1 (Snd Driver timer) 1.2766 µs

VIA timer 2 (Disk Driver timer) 1.2766 µs

Time Manager (original) 1 ms

Time Manager (revised or extended) 20 µs

Interestingly, they are virtually all the same thing, much as a single timepiece might have two or more hands running at different speeds. The VIA chip provides a high frequency heartbeat; the basis for most timed operations on the Mac. All the above timers derive from the VIA’s clock of 1.2766 µs period. As mentioned above, Time and Ticks are scheduled by the VIA for regular updating. VIA timers 1 and 2 are memory mapped registers available for applications to poke values into, which are subsequently decremented by the VIA (the method used by Think C’s profiler package). The Time Manager uses these same registers itself.

So which incarnation to use? Code executes relatively quickly, and what we want to see are small differences. We need the highest resolution timer available. There are two arguments for the extended Time Manager. The TM is likely to continue to be supported by Apple, even if the underlying hardware changes, possibly even away from the VIA chip altogether. More importantly, the TM incorporates a partial interrupt compensation mechanism. It’s somewhat more stable than any other choice.

A Better Method

As discussed above, measurements of execution time are in error by an amount that fluctuates from trial to trial. Since the problem is that of the timer continuing to run, even while Foo is temporarily suspended, the measurements of execution time are typically too large. If we measure over many trials, we collect a distribution of times, with some sort of overall average time. This average time is still not the actual execution time-it is also too large. Thankfully, we do not care about the absolute time, which would be very difficult to extract. Rather, in comparing Foo to FooBar, we take the difference of averages. Most of the error is subtracted away, without ever needing to know a specific magnitude of the error. This works as long as the (unknown) error is close to the same average size for both functions. Don’t be fooled! Without a feeling for whether the fluctuations have been reasonably averaged, and in the same way for both functions, we would know nothing. This is what the tools provided are all about-checking the quality and sameness of the time distributions for Foo and FooBar.

The process breaks up into two steps (also two files). Step one (GatherTimes.c) is to gather the best raw timing data we can, and accumulate it into the statistical package. Some systematic errors are also corrected in this step. Step two (TStats.c) is to analyze the data using plots of the distributions and appropriate statistical measures.

Gathering Data

GatherTimes.c has a nested loop structure. The outer is the accuracy loop. Each iteration of this loop collects two raw times, time1 and time2, for Foo and FooBar respectively. The times are passed to the TSAccumulate routine of the TStats file. The number of iterations determines the size of the statistical sample. Accuracy in statistical lingo refers to how closely a value approximates the true value, or how closely a sample population represents the true “parent” population. We need enough data to get an accurate portrait of a distribution. Something between 50 and 1000 is a reasonable sample size for most cases.

On a given pass through the accuracy loop, we measure the time for Foo as follows. Initialize any data to be used by Foo, as necessary. Call Foo once, to fill the instruction and data caches as much as possible with Foo stuff. Start the TM clock by installing and priming a TMTask record. Execute Foo one or more times, to be explained shortly. Remove the TMTask. The difference between the tmCount field of the removed record and the time parameter passed to PrimeTime is the elapsed raw time. FooBar gets the same treatment.

The inner loop determines how many times to execute Foo while the timer is running. This is the precision loop. Precision is a notion that complements accuracy, expressing how many significant digits there are in a number. Precision is something like the inherent resolving power of an instrument, independent of how carefully one uses the instrument. The times we collect are integers, known (according to the TM documentation) to ±20µs. The more iterations in the inner loop, the larger our collected times. The more digits we have to work with, the more clearly we can see small relative differences. Choosing a good value for precision is a matter of balancing two things. On the one hand, you want times large enough to see detail of the size you are interested in. You need bigger numbers to confidently see 1% differences than you need for 100% differences. On the other hand, the more iterations, the more fluctuation errors creep in. You have to develop your own sense of what’s happening in your situation. It takes experimentation with different settings of precision-it’s a skill that has to be learned.

This is the right time to mention that there is an important pair of experiments you should do whenever beginning timing work on a new set of functions. They are useful for choosing precision wisely. Try timing Foo against Foo (the identical function), where you know the difference is supposed to be zero. You will notice that the difference is usually not zero, but close to it. Alternatively, time Foo against Foo2 (a duplicate function, not the identical one). Here you may find a larger difference. Probably the best you can hope for is a relative difference of about 1%. These experiments help you feel for what kinds of results to expect, and what your resolution limit is. This is the reason for the macros Fun1 and Fun2 near the top of GatherTimes.c. They make it simple to define which functions you’re running from trial to trial.

Continuing now with the gathering of raw times, we make a further systematic error correction. GatherTimes assumes that the argument lists for Foo and FooBar are identical, (and it has to be defined in the ArgList macro at the top). Since we want to time the guts of these functions, and not the calling time to set up the stack, we time a dummy function, Overhead. Overhead takes the same arguments as Foo and FooBar, and just returns. Also, since it is similarly measured with the TM, this determines the time it takes simply to start and stop the TM clock. This is actually the larger of the two errors. This overhead time is subtracted from each of time1 and time2 before sending them to TSAccumulate.

Understanding The Data

Now comes the analysis. The results window is divided into four areas. The lower two are plots of time1 and time2 respectively. The upper-left reports numerical statistics for all of the raw data for both time1 and time2. The upper-right reports similar numerical results, but for a subset of the raw data, the modes. I’ll explain this in a minute. TStats has a number of calls for displaying these areas. We’ll walk through each one. The sequence of these calls that I find most useful is demonstrated in the GatherTimes file. Note that WAIT is a simple macro for while( !Button() ) do nothing. It’s just a quick and dirty way of walking through the displays at your own pace.

Figure 2 Typical Raw Data

TSRawPlots generates graphs of the raw times, in the order they were collected. The times are collected in arrays of longs (raw1 and raw2), by the TSAccumulate function. The horizontal axis is just the index of the entries, from zero to whatever you set for accuracy (the number of entries). The vertical axis is time (µs in this case). This type of plot is the least useful for analysis, but it’s interesting. You can see the different sizes of fluctuations and how often they occur. A red line is drawn at the average time for all entries.

TSRawHistos does the real work. It creates what are called alternatively: histograms, frequency distributions, or frequency spectra of the raw time arrays. Let’s take the array raw1 as an example. What we do is simple. We find the minimum and maximum of all the time entries in raw1. This range is divided into N buckets or bins, (a new array called bins1 with N entries), which are initialized to zero. Each bin now represents a narrow time range. As we walk through the entries of raw1, we test which bin the entry falls into, say bin k. We increment bins1[k] by one. Doing this for all of the entries of raw1 results in a description of how frequently times of each size are observed. Hence, we form a frequency distribution. TSRawHistos then plots the bins arrays. The horizontal axis is an index in a bins array, which now represents time. The vertical axis is counts.

Figure 3 Typical Frequency Distributions

Sometimes the shapes of the frequency distributions are very symmetrical, and close to Normal. Sometimes they are quite asymmetrical, and even have two or more peaks. This is not a big problem. The most important thing to look for is that the two distributions have a similar shape to each other. If one has its peak bin to the left of center, with a long tail on the right, that’s O.K., if the other is skewed the same way. The similarity of shape gives us some confidence that the average error is similar, and will be subtracted away when we take the difference. If the shapes are clearly different, toss the data out and try again. Always run a few trials anyway, because things do change a little.

TSStats calculates and displays numerical results which describe the distributions. Generally, distributions come in many different shapes, depending on the underlying process at work. Some are bell shaped, some are flat, some are triangles... If it looks something like a bell, that is, having a peak, and getting progressively closer to zero as you go away from the peak on either side, then there are some standard characterizations that are meaningful to apply. These are: the location of the peak and the variability, or “width” of the distribution.

There are many possible choices for describing where a peak is: arithmetic mean, geometric mean, harmonic mean, median, mode, ... We make use of two: the arithmetic mean, which is the simple average, and the mode, which singles out the most frequent value, ignoring the rest. Why these? Among the three types of mean, the geometric and harmonic place slightly stronger emphasis on smaller values. We have no reason to discount our larger values on principle. The median divides a distribution in half. Half of the values are smaller, and half larger than the median. This is more appropriate for flatter and broader curves than what we usually see. The mode is probably the best choice. I find that 40% or more of all the values are of one size, so the peak is very sharply defined at the mode. I also find that when timing Foo against itself, the modes of time1 and time2 are often the closest together of all estimators. Lastly, since we throw out all data besides the mode, we are less dependent on the full distributions having similar shapes. Rather than rely on just one estimator, we show results using means in the left numerical results box, and modes in the right box. Also calculated from these are the difference (1-2), and the relative difference (1-2)/2, being the number we most want to know.

The other thing to discuss is the width of a distribution. We calculate this in the traditional way, as the standard deviation (sd) of the times. The sd is the square-root of another quantity, the variance. The variance is defined as the average of the squared differences from the mean. It tells us about how spread out a distribution is. The narrower the distribution, the better we believe we can locate a peak successfully, or meaningfully, hence, the more confident we are of the results we have extracted. The boxes show the standard deviations for all of raw1 and raw2 (left box), and for the values in the mode bins only (right box). Try moving the mouse around during a timing run. Watch how the standard deviations in the left box increase as you thereby generate more interrupt activity. Also, your distributions will become multimodal (having several peaks). These peaks point out the extra work required to recalculate the cursor location.

Figure 4 Raw Data - Mouse Moved

Figure 5 Distributions- Mouse Moved

Figure 6 Modes- Mouse Moved

The standard deviation is not very interesting by itself, but it is used in calculating a confidence limit (Z) for our results. Z is defined as |difference of means|/sd, where |x| is the absolute value of x. It reflects how separated two values are in comparison to the intrinsic variability of the values. This is how we can attach a measure of significance to the finding that two means differ from one another. Z is interpreted in the following way. Suppose we are measuring some quantity t. Thanks to a fundamental statistical result called the Central Limit Theorem, whether or not t itself is Normally distributed, it’s mean <t> is Normal if the sample size is large enough (N > 50 is adequate). Further, the difference of two means is also Normal. Now, being Normally distributed is a way of saying that <t> fluctuates, by chance alone. One computes Z as above, and asks what is the probability that a Z as large as ours would be found just by chance, due to random fluctuations, and not because the means are really and truly different. This is answered by comparing your Z against a table of probabilities for various Zs (a table of integrated tail areas under the Normal curve). The following is a selection of such values.

Z Prob. of Z this large by chance alone

0.2 84%

0.5 62%

1.0 32%

1.5 13%

2.0 5%

For example, if your Z is about 2.0, then you proclaim, FooBar is thus and such percent faster than Foo with a 95% confidence limit, and your scientifically minded friends applaud and yell “Publish, Publish!” In most scientific circles, if Z is less than 1.5 or so, then you’ve found squat. This is a pretty good rule of thumb. Your means should be at least 2 standard deviations apart, or you’re probably looking at noise and little else.

TSFilterMode calculates the modes for time1 and time2. It scans input arrays for the values falling in the mode bins, and copies these data to other arrays, work1 and work2. You can specify that input be taken from the raw arrays, which you would do on a first pass only, or from the existing work arrays on subsequent passes. Note that you might have to make several passes at the mode if your number of bins is small (i.e.., bin widths are large and capture more than one time magnitude). You can tell if the current pass finished the job by getting a return value of TRUE, or by noting that the standard deviations displayed in the right box are both zero. If you have the true mode, you have a set of values which are all the same, hence, have zero variation. Checking for purity of the mode is the reason for reporting the standard deviations in the right box at all. The Z reported on the right still uses the sd results of the full distribution. All we have changed on the right is how the peaks have been determined.

Summary

Which is better, means or modes? Look for the larger Z, and for the most consistent result from trial to trial. You should also check which gives a better “zero” when timing a function against itself. Finally, you might be asking “Does it really take this much effort to find out something so simple?” Well, is there anything worth knowing that doesn’t?

/* 1 */
Source code Copyright (c) 1993 Bill Karsh. All rights reserved.

/* File GatherTimes
Demo precision timing experiments.  We collect raw time measurements, 
then call the TStats package to display results.
For your functions, you must:
 1) #include their headers.
 2) #define Arglist, Fun1, Fun2 macros with the specs for your functions 
(as shown for Foo and FooBar).
 3) declare and init data needed by your functions in main.
 4) init anything else your functions need before the Loop macro (as 
we set arg1 = arg2 = 1).

You need not modify anything else except the testing parameters {Which, 
Precision, Accuracy}.
*/

#pragma options( !check_ptrs )

#include"TestFuncs.h"
#include"TStats.h"
#include<Timer.h>

// competitors to test

#define First    1
#define Second   2
#define Both3

#define Which    Both

#define ArgList  ( &arg1, &arg2 )
#define Fun1Foo
#define Fun2FooBar

//------------------------------------------------------------

// timing parameters

#define MaxNeg   0x80000000

#define Precision100
#define Accuracy 1000// must be > 0

//------------------------------------------------------------

// timing macros and glue

static void Overhead( ... )
{
 // always empty
}

// call once to fill caches
// call repeatedly to gather timing data

#define Loop( F, T ) \
 F ArgList; \
 \
 tmt.tmWakeUp = tmt.tmReserved = 0L; \
 InsXTime( &tmt ); \
 PrimeTime( &tmt, MaxNeg ); \
 for( prc = 0; prc < Precision; prc++ ) {    \
 F ArgList; \
 } \
 RmvTime( &tmt );\
 T = tmt.tmCount - MaxNeg
 
#define WAIT\
 Delay( 20, &dum ); while( !Button() )
 
//------------------------------------------------------------

void main( void )
{
//--- specific args for your functions

 long arg1, arg2;
 
//--- timer args

 long prc, acc;
 long time1 = 0, time2 = 0, timeOv;
 long dum;
 TMTask tmt;
 Booleandone;
 
// initializations

 InitGraf( &thePort );
 InitFonts();
 InitWindows();
 InitMenus();
 TEInit();
 InitDialogs( nil );
 InitCursor();
 
 tmt.tmAddr = nil;
 
 TSInit( nil, Accuracy, Accuracy );

//------------------------------------------------------------

 for( acc = 0; acc < Accuracy; acc++ ) {
 
#if Which & First
 // init data for your Fun1
 arg1 = 1;
 arg2 = 1;
 
 Loop( Fun1, time1 );
#endif


#if Which & Second
 // init data for your Fun2
 arg1 = 1;
 arg2 = 1;
 
 Loop( Fun2, time2 );
#endif

 Loop( Overhead, timeOv );
 if( time1 > timeOv ) time1 -= timeOv;
 if( time2 > timeOv ) time2 -= timeOv;
 
 TSAccumulate( time1, time2 );
 }
 
//------------------------------------------------------------

 TSRawPlots();
 TSStats( kRaw );
 WAIT;
 
 TSRawHistos();
 WAIT;
 
 done = TSFilterMode( kRaw );
 TSStats( kWork );
 WAIT;
 
 if( !done ) {
 do {
 done = TSFilterMode( kWork );
 TSStats( kWork );
 WAIT;
 } while( !done );
 }
 
 TSDispose();
}

/* File TStats -----------------------------------------------
 Accumulate, calculate and display timing data.
*/
 
#pragma options( honor_register, !assign_registers )
#pragma options( !check_ptrs )

#include"TStats.h"
#include"LongArrayStats.h"
#include"PlotLongArray.h"
#include<math.h>
#include<stdio.h>

#define WMargins 5
#define TitleBarHt 18
#define TextLines3
#define UseSameScales1

// glue and shorthands

#define Alloc( type, n )  \
 (type*)NewPtr( sizeof(type) * (n) )
 
#define Kill( q )\
 if( ts->q.data ) DisposePtr( ts->q.data )
 
#define Limits( q )\
 LongArrayMinMax( ts->q.data, ts->q.N, \
 &ts->q.min, &ts->q.max )
 
#define Plot( q, r, str ) \
 PlotLongArray( ts->q.data, ts->q.N, \
 0, ts->q.N,ts->q.min, ts->q.max,  \
 &ts->r, str )
 
#define Histo( h, q )\
 LongArrayBin( ts->q.data, ts->q.N,\
 ts->q.min, ts->q.max,    \
 ts->h.data, ts->h.N )

#define SameScales( u, v )\
 if( ts->u.min < ts->v.min )\
 ts->v.min = ts->u.min;   \
 else   \
 ts->u.min = ts->v.min;   \
 \
 if( ts->u.max > ts->v.max )\
 ts->v.max = ts->u.max;   \
 else   \
 ts->u.max = ts->v.max;
 
 
#define MaxBin( q, h, w ) \
 LongArrayGetMaxBin( ts->q.data, ts->q.N,\
 ts->q.min, ts->q.max,    \
 ts->h.data, ts->h.N,\
 ts->w.data, &ts->w.N )
 
#define NewPort()\
 GetPort( &oldPort ); SetPort( ts->w )
 
#define Print( h, str, val, dig )  \
 MoveTo( h, v ); \
 *s = sprintf( s+1, "%."#dig"f", val );\
 DrawString( str ); DrawString( s )

 
static pTSgTS;

/* LayoutWindow ----------------------------------------------
 Arrange data areas of window.
*/

static void LayoutWindow( void )
{
 register pTS  ts = gTS;
 Rect   r;
 FontInfo fi;
 short  lineHt, pad;

 r = ts->w->portRect;
 InsetRect( &r, WMargins, WMargins );

 GetFontInfo( &fi );
 lineHt = fi.ascent + fi.descent + fi.leading;

#define t1 ts->statsR1
#define t2 ts->statsR2
#define p1 ts->plotR1
#define p2 ts->plotR2

// left and right

 t1.left  = p1.left  = p2.left  = r.left;
 t2.right = p1.right = p2.right = r.right;

 t1.right = t1.left  + (r.right - t1.left - WMargins)/2;
 t2.left  = t1.right + WMargins;

// top and bottom

 t1.top    = t2.top    = r.top;
 t1.bottom = t2.bottom = t1.top + TextLines * lineHt + 2;
 
 p1.top    = t1.bottom + WMargins;
 p1.bottom = p1.top + (r.bottom - p1.top - WMargins)/2;
 
 p2.top    = p1.bottom + WMargins;
 p2.bottom = r.bottom;

#undef t1
#undef t2
#undef p1
#undef p2
}

/* AllocateArrays ------------------------------------------*/

static void AllocateArrays( long nData, long nBins )
{
 register pTS  ts = gTS;
 
 ts->maxRaw  = nData;
 ts->bins1.N= ts->bins2.N = nBins;
 
 ts->acc1 = ts->raw1.data = Alloc( long, nData );
 ts->acc2 = ts->raw2.data = Alloc( long, nData );
 
 ts->bins1.data = Alloc( long, nBins+1 );
 ts->bins2.data = Alloc( long, nBins+1 );
 
 ts->work1.data = ts->work2.data = nil;
 
 ts->raw1.N = ts->raw2.N = 0;
}

/* TSInit ----------------------------------------------------
 Allocate window and init structures.
*/

void TSInit( Rect *rGlobal, long nData, long nBins )
{
 register pTS  ts;
 GrafPtroldPort;
 Rect   *r, R;
 
 gTS = ts = Alloc( TSRec, 1 );
 
 if( !(r = rGlobal) ) { // auto size window
 R = screenBits.bounds;
 InsetRect( &R, 3, 3 );
 R.top += MBarHeight + TitleBarHt;
 R.bottom >>= 1;
 r = &R;
 }
 
 ts->w = NewWindow( nil, r, nil, true,
 noGrowDocProc, (WindowPtr)-1, false, 0L );
 
 NewPort();
 TextFont( geneva );
 TextSize( 9 );
 
 LayoutWindow();
 
 AllocateArrays( nData, nBins );
 
 ts->combRawSd = 0.0;
 
 SetPort( oldPort );
}

/* TSDispose -------------------------------------------------*/

void TSDispose( void )
{
 register pTS  ts = gTS;
 
 if( !ts ) return;
 
 if( ts->w ) DisposeWindow( ts->w );
 
 Kill( raw1 );
 Kill( raw2 );
 Kill( work1 );
 Kill( work2 );
 Kill( bins1 );
 Kill( bins2 );
 
 DisposePtr( ts );
}

/* TSAccumulate ----------------------------------------------
 Add data to arrays.
*/

void TSAccumulate( long time1, long time2 )
{
 register pTS  ts = gTS;

 if( ts->raw1.N < ts->maxRaw && time1 >= 0 ) {
 *ts->acc1++ = time1;
 ++ts->raw1.N;
 }
 
 if( ts->raw2.N < ts->maxRaw && time2 >= 0 ) {
 *ts->acc2++ = time2;
 ++ts->raw2.N;
 }
}


/* TSRawPlots ------------------------------------------------
 Display plots of accumulated raw data.
*/

void TSRawPlots( void )
{
 register pTS  ts = gTS;
 GrafPtroldPort;
 
 if( !ts->raw1.N ) return;
 
 NewPort();
 
 Limits( raw1 );
 Limits( raw2 );
 
#if UseSameScales == 1
 SameScales( raw1, raw2 );
#endif
 
 Plot( raw1, plotR1, "\pRaw Time1" );
 Plot( raw2, plotR2, "\pRaw Time2" );
 
 SetPort( oldPort );
}


/* TSStats ---------------------------------------------------
 Calculate and display statistics for arrays.
 sourceType is one of the defined constants {kRaw, kWork}.
 */

void TSStats( long sourceType )
{
 register pTS  ts = gTS;
 GrafPtroldPort;
 Rect   *r;
 double mean1, mean2, sd1, sd2, z1, z2;
 long   *data1, *data2;
 long   N1, N2;
 FontInfo fi;
 Byte   s[36];
 short  lineHt, h1, h2, h3, v;
 
 if( sourceType == kRaw ) {
 data1 = ts->raw1.data;
 data2 = ts->raw2.data;
 N1 = ts->raw1.N;
 N2 = ts->raw2.N;
 r = &ts->statsR1;
 }
 else {
 data1 = ts->work1.data;
 data2 = ts->work2.data;
 N1 = ts->work1.N;
 N2 = ts->work2.N;
 r = &ts->statsR2;
 }
 
 if( !N1 ) return;
 
 NewPort();
 
 ForeColor( blackColor );
 EraseRect( r );
 FrameRect( r );
 
 GetFontInfo( &fi );
 h1 = r->left + 2;
 h3 = (r->right - r->left)/3;
 h2 = r->left + h3;
 h3 += h2;
 v  = r->top + fi.ascent + 1;
 
 lineHt = fi.ascent + fi.descent + fi.leading;
 
 LongArrayMeanDev( data1, N1, &mean1, &sd1 );
 LongArrayMeanDev( data2, N2, &mean2, &sd2 );
 
 if( sourceType == kRaw ) {
 Print( h1, "\pmean1 = ", mean1, 0 );
 Print( h2, "\pmean2 = ", mean2, 0 );
 }
 else {
 Print( h1, "\pmode1 = ", mean1, 0 );
 Print( h2, "\pmode2 = ", mean2, 0 );
 }
 
 v += lineHt;
 
 Print( h1, "\psd1 = ", sd1, 2 );
 Print( h2, "\psd2 = ", sd2, 2 );
 
 v += lineHt;
 
 z1 = mean1 - mean2;
 Print( h1, "\pdiff = ", z1, 0 );
 
 z2 = z1 / mean2 * 100.0;
 Print( h2, "\prel diff = ", z2, 2 );
 DrawChar( '%' );
 
 if( sourceType == kRaw ) {
 
 ts->combRawSd = z2 =
 sqrt( sd1*sd1/ts->raw1.N + sd2*sd2/ts->raw2.N );
 
 if( z2 > 0.0 )
 z1 = fabs( z1 ) / z2;
 else
 z1 = 0.0;
 Print( h3, "\pZ = ", z1, 2 );
 }
 else {
 if( ts->combRawSd > 0.0 )
 z1 = fabs( z1 ) / ts->combRawSd;
 else
 z1 = 0.0;
 Print( h3, "\pZ = ", z1, 2 );
 }
 
 SetPort( oldPort );
}


/* TSRawHistos -----------------------------------------------
 Calculate and display plots of freq data.
*/

void TSRawHistos( void )
{
 register pTS  ts = gTS;
 GrafPtroldPort;

 if( !ts->raw1.N ) return;
 
 NewPort();
 
 Histo( bins1, raw1 );
 Histo( bins2, raw2 );
 
 Limits( bins1 );
 Limits( bins2 );
 
#if UseSameScales == 1
 SameScales( bins1, bins2 );
#endif

 Plot( bins1, plotR1, "\pFreq Time1" );
 Plot( bins2, plotR2, "\pFreq Time2" );
 
 SetPort( oldPort );
}

/* TSFilterMode ----------------------------------------------
 Calculate and display plots of data only in max bin ( the mode ).
 Places this separated data in work structures.
 sourceType is one of the defined constants {kRaw, kWork}.
 Returns true if max == min for newly filtered data, else returns false.
*/

Boolean TSFilterMode( long sourceType )
{
 register pTS  ts = gTS;
 GrafPtroldPort;
 
 if( !ts->raw1.N ) return true;
 
 NewPort();
 
 if( !ts->work1.data )
 ts->work1.data = Alloc( long, ts->raw1.N );
 
 if( !ts->work2.data )
 ts->work2.data = Alloc( long, ts->raw1.N );
 
 if( sourceType == kRaw ) {
 MaxBin( raw1, bins1, work1 );
 MaxBin( raw2, bins2, work2 );
 }
 else {
 if( ts->work1.max != ts->work1.min )
 MaxBin( work1, bins1, work1 );
 
 if( ts->work2.max != ts->work2.min )
 MaxBin( work2, bins2, work2 );
 }
 
 Limits( work1 );
 Limits( work2 );
 
 Histo( bins1, work1 );
 Histo( bins2, work2 );
 
 Limits( bins1 );
 Limits( bins2 );
 
 Plot( bins1, plotR1, "\pMost Freq Time1" );
 Plot( bins2, plotR2, "\pMost Freq Time2" );
 
 SetPort( oldPort );
 
 return (ts->work1.max == ts->work1.min &&
 ts->work2.max == ts->work2.min);
}


/* File LongArrayStats -------------------------------------------
 Calculate various statistics for arrays of longs.
*/
 
#pragma options( honor_register, !assign_registers )

#include"LongArrayStats.h"
#include<math.h>


/* LongArrayMinMax -------------------------------------------
 Calculate minimum and maximum values.
*/

void LongArrayMinMax(
 register long *dp,
 register long N,
 long   *min,
 long   *max )
{
 register long mx = 0x80000000, mn = 0x7fffffff, d;
 
 if( !N ) {
 *min = *max = 0;
 return;
 }
 
 do {
 d = *dp++;
 if( d < mn ) mn = d;
 if( d > mx ) mx = d;
 } while( --N );
 
 *min = mn;
 *max = mx;
}


/* LongArrayMeanDev ------------------------------------------
 Calculate array's mean and standard deviation.
*/

void LongArrayMeanDev(
 register long *dp,
 long   N,
 double *mean,
 double *sd )
{
 register long n = N, d;
 register double sumX, sumX2;
 
 sumX = sumX2 = 0;

 if( n ) {
 
 do {
 d = *dp++;
 sumX += d;
 sumX2  += d * d;
 } while( --n );
 
 n = N;
 
 sumX = sumX / n;
 sumX2  = sumX2 / n - sumX * sumX;
 if( n > 1 ) sumX2 *= n / (n - 1);
 sumX2 = sqrt( sumX2 );
 }
 
 *mean = sumX;
 *sd = sumX2;
}


/* LongArrayBin ----------------------------------------------
 Bin data array into a bins array.
  If input bins is nil, this routine allocates the bins array.
*/

long *LongArrayBin(
 register long *data,
 register long N,
 register long min,
 long   max,
 long   *bins,
 register long nBins )
{
 register long *b;
 register long n, span;
 
 if( !bins )
 bins = (long*)NewPtr( sizeof(long)*(nBins + 1) );
 
 if( !(b = bins) ) goto exit;
 
// zero bins array
 
 n = nBins + 1;
 
 do {
 *b++ = 0;
 } while( --n );
 
// bin data
 
 b = bins;

 if( span = max - min ) {
 
 do {
 b[((*data++ - min) * nBins) / span]++;
 } while( --N );
 
 b[nBins - 1] += b[nBins];
 }
 else
 b[0] = N;
 
exit:
 return bins;
}


/* LongArrayGetMaxBin ----------------------------------------
 Using an array {in} of data, and its corresponding array {bins}, return 
in array {out}, only those data falling in maximum height bin.  in and 
out can be the same array.
*/

void LongArrayGetMaxBin(
 long   *in,
 register long nIn,
 register long min,
 long   max,
 long   *bins,
 register long nBins,
 long   *out,
 long   *nOut )
{
 register long *insert, *look;
 register long newN, maxCounts;
 short  maxBinNum, iBin, pad;
 
// quick exits

 if( !nIn ) {
 *nOut = 0;
 return;
 }
 
 if( max == min ) {
 *nOut = nIn;
 if( in != out ) {
 do {
 *out++ = *in++;
 } while( --nIn );
 }
 return;
 }
 
// find max bin

 maxBinNum= 0;
 maxCounts= -1;
 look   = bins;
 
 for( iBin = 0; iBin < (short)nBins; iBin++, look++ ) {
 if( *look > maxCounts ) {
 maxCounts = *look;
 maxBinNum = iBin;
 }
 }

// replace data with data in max bin

 newN = 0;
 look = in;
 insert = out;
 max    -= min;
 
 if( maxBinNum == nBins - 1 ) {
 // edge condition

 do {
 if( ((*look - min)*nBins)/max >= maxBinNum ) {
 *insert++ = *look;
 newN++;
 }
 look++;
 } while( --nIn && newN < maxCounts );
 }
 else {

 do {
 if( ((*look - min)*nBins)/max == maxBinNum ) {
 *insert++ = *look;
 newN++;
 }
 look++;
 } while( --nIn && newN < maxCounts );
 }
 
 *nOut = newN;
}


/* File PlotLongArray ----------------------------------------
 Plot array of longs.
*/

#pragma options( honor_register, !assign_registers )
#include"PlotLongArray.h"
#define TextMargin 1

/* PlotLongArray ---------------------------------------------
 Plot array of N longs.
 min and max values set the scales.
 r bounds the entire plot including labels.
*/
 
#pragma options( honor_register, !assign_registers )
#include"PlotLongArray.h"

void PlotLongArray(
 register long *data,
 long   N,
 long   hMin,
 long   hMax,
 long   vMin,
 long   vMax,
 Rect   *r,
 StringPtrtitle )
{
 register short  v0, wHi, hScale, vScale;
 register long i, sum;
 short  h, wWid, sMinWid, sMaxWid;
 Rect   R;
 Point  p;
 Byte   sMin[16], sMax[16];
 FontInfo fi;
 
// adjust vMin and vMax

 if( vMin > 0 && vMax > 0 )
 vMin = 0;
 else if( vMin < 0 && vMax < 0 )
 vMax = 0;
 
 R = *r;
 EraseRect( &R );
 
// make room for labels on left and bottom
 
 NumToString( vMin, sMin );
 NumToString( vMax, sMax );
 GetFontInfo( &fi );
 sMinWid = StringWidth( sMin );
 sMaxWid = StringWidth( sMax );
 h = sMaxWid;
 if( sMinWid > h ) h = sMinWid;
 
 R.left += h + TextMargin*2;
 R.bottom -= fi.ascent + fi.descent + TextMargin;
 
 ForeColor( greenColor );
 FrameRect( &R );
 InsetRect( &R, 1, 1 );

// vert labels
 
 ForeColor( blackColor );
 MoveTo( R.left - sMaxWid - TextMargin,
 R.top + fi.ascent );
 DrawString( sMax );
 
 MoveTo( R.left - sMinWid - TextMargin,
 R.bottom - fi.descent );
 DrawString( sMin );
 
// horiz labels
 
 v0 = R.bottom + fi.ascent + TextMargin;
 NumToString( hMin, sMin );
 MoveTo( R.left + TextMargin, v0 );
 DrawString( sMin );
 
 NumToString( hMax, sMax );
 MoveTo( R.right - StringWidth( sMax ) - TextMargin, v0 );
 DrawString( sMax );
 
 if( title ) {
 MoveTo((R.right + R.left - StringWidth(title))/2, v0);
 DrawString( title );
 }
// get ready to plot

 hScale = hMax - hMin;
 vScale = vMax - vMin;

 if( !hScale || !vScale ) return;
 
 wWid = R.right - R.left;
 wHi    = R.bottom - R.top;

// draw absissa at v = 0
 
 v0 = R.top + (vMax * wHi) / vScale;
 
 MoveTo( R.left, v0 );
 LineTo( R.right - 1, v0 );
 
// draw data

 sum = 0;
 
 for( i = 0; i < N; i++ ) {
 
 h = R.left + (i * wWid) / hScale;
 
 MoveTo( h, v0 );
 LineTo( h, v0 - (*data * wHi) / vScale );
 
 sum += *data;
 data++;
 }

// draw average v line

 v0 -= (sum/N * wHi) / vScale;
 
 ForeColor( redColor );
 MoveTo( R.left, v0 );
 LineTo( R.right - 1, v0 );
 
 ForeColor( blackColor );
}

/* File TestFuncs --------------------------------------------
 Functions to be timed.
*/

#include"TestFuncs.h"

void Foo( long *x, long *y )
{
 long i = 5;
 
 do {
 *x *= *y;
 } while( --i );
}


void FooBar( long *x, long *y )
{
 long i = 5;
 
 do {
 *x *= *y;
 } while( --i );
}







  
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Adobe Acrobat Reader 20.012.20041 - View...
Adobe Acrobat Reader allows users to view PDF documents. You may not know what a PDF file is, but you've probably come across one at some point. PDF files are used by companies and even the IRS to... Read more
Adobe Acrobat DC 20.012.20041 - Powerful...
Acrobat DC is available only as a part of Adobe Creative Cloud, and can only be installed and/or updated through Adobe's Creative Cloud app. Adobe Acrobat DC with Adobe Document Cloud services is... Read more
Sketch 68 - Design app for UX/UI for iOS...
Sketch is an innovative and fresh look at vector drawing. Its intentionally minimalist design is based upon a drawing space of unlimited size and layers, free of palettes, panels, menus, windows, and... Read more
Bean 3.3.1 - Fast and uncluttered word p...
Bean is no longer being actively developed, but will be updated as necessary to patch bugs and maintain OS X compatibility Bean is lean, fast, and uncluttered. If you get depressed at the thought... Read more
RetroArch 1.9.0 - Game emulator.
RetroArch is most popularly known for being a program with which you can play many emulators and games, which have all been customized and tailor-ported to the libretro API. It is designed to be fast... Read more
NetNewsWire 5.0.4 - RSS and Atom news re...
NetNewsWire is the best way to keep up with the sites and authors you read most regularly. Let NetNewsWire pull down the latest articles, and read them in a distraction-free and Mac-like way. Native... Read more
EarthDesk 7.4.5 - $24.99
EarthDesk replaces your static desktop picture with a rendered image of Earth showing correct sun, moon, and city illumination. With an Internet connection, EarthDesk displays near-real-time global... Read more
BetterTouchTool 3.401 - Customize multi-...
BetterTouchTool adds many new, fully customizable gestures to the Magic Mouse, Multi-Touch MacBook trackpad, and Magic Trackpad. These gestures are customizable: Magic Mouse: Pinch in / out (zoom)... Read more
Vienna 3.5.6 :e12c952d: - RSS and Atom n...
Vienna is a freeware and Open-Source RSS/Atom newsreader with article storage and management via a SQLite database, written in Objective-C and Cocoa, for the OS X operating system. It provides... Read more
WhatsApp 2.2031.5 - Desktop client for W...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more

Latest Forum Discussions

See All

Scrappers receives a major update that a...
Q-Games' Scrappers has received a fairly sizeable new update that adds fresh gameplay features and a host of quality-of-life tweaks. [Read more] | Read more »
Motorball is a car football game from No...
A few years back Noodlecake Studios announced that they would be dipping in the multiplayer gaming realm with two different games. The first of those, Golf Blitz, released a while back and has proven to be very popular. Now, the second has arrived... | Read more »
SINoALICE's latest update introduce...
SINoALICE's latest update has now arrived, adding several fan-favourite characters from popular RPG series NieR. Young Nier, Kaine, and Young Emil are available in-game as part of a limited-time crossover event set to run until August 20th. [Read... | Read more »
Rocat Jumpurr is an intense roguelite pl...
Rocat Jumpurr is a roguelite platformer from developer Mousetrap Games. You might already be familiar with it if you follow the Big Indie Pitch, where it won first place during this year's Pocket Gamer Connects London competition. Following its... | Read more »
PUBG Mobile's Play As One campaign...
Back in mid-July, we reported that PUGB Mobile had teamed up with Direct Relief to help raise money for the charity's COVID-19 response project. It focused on an in-game running challenge for players, which lead to the PUBG Mobile donating $2... | Read more »
Marvel Contest of Champions' latest...
Marvel Contest of Champions' latest motion comic has arrived, and it shows off new fighters Air-Walker and Dragon Man. Both characters are set to arrive in-game this month. [Read more] | Read more »
Clash Royale: The Road to Legendary Aren...
Supercell recently celebrated its 10th anniversary and their best title, Clash Royale, is as good as it's ever been. Even for lapsed players, returning to the game is as easy as can be. If you want to join us in picking the game back up, we've put... | Read more »
Global Spy is an intriguing 2D spy sim f...
Developer Yuyosoft Innovations' Global Spy launched last month for iOS and Android, though if you missed it at the time, we're here to tell you why it's well worth a go. This one's all about international espionage, tracking down elusive spies,... | Read more »
Distract Yourself With These Great Mobil...
There’s a lot going on right now, and I don’t really feel like trying to write some kind of pithy intro for it. All I’ll say is lots of people have been coming together and helping each other in small ways, and I’m choosing to focus on that as I... | Read more »
Hyena Squad is sci-fi turn-based strateg...
Wave Light Games has just revealed its latest release, Hyena Squad, a turn-based RPG set in a space station infested by gross aliens and the living dead. The announcement was first reported on by Touch Arcade. [Read more] | Read more »

Price Scanner via MacPrices.net

Apple drops prices on clearance 27″ 5K iMacs,...
Apple has dropped prices on Certified Refurbished 2019 27″ iMacs to a new low of $1439 and up to $520 off their original MSRP. Apple’s one-year warranty is standard and shipping is free. The... Read more
Price drop: Clearance 8-core iMac Pro for $38...
Apple has dropped their price on Certified Refurbished 27″ 3.2GHz 8-Core iMac Pros to $3819 including free shipping. Their price is $1180 off the original MSRP of new models. A standard Apple one-... Read more
Monday sale: New 13″ 2.0GHz MacBook Pros for...
Amazon has new 2020 13″ 2.0GHz/512GB MacBook Pros back in stock on sale today for $200 off Apple’s MSRP. Shipping is free. Be sure to purchase the MacBook Pro from Amazon, rather than a third-party... Read more
Sale! Apple’s 16″ MacBook Pros for up to $349...
Apple Authorized Reseller Adorama has new 2019 16″ MacBook Pros in stock on sale today for $100-$349 off Apple’s MSRP, each including free shipping. Their prices for 8-core models ($349 off) are the... Read more
Save hundreds of dollars on a custom-configur...
Save up to $920 on a custom-configured 16″ MacBook Pro with these Certified Refurbished models that Apple has restocked today. Each MacBook Pro features a new outer case, free shipping, and includes... Read more
New 2020 12.9″ iPad Pros on sale for up to $8...
Apple reseller Expercom has new 2020 Apple 12.9″ iPad Pros on sale today for $60-$85 off MSRP, with prices starting at $939. These are the same iPad Pros sold by Apple in their retail and online... Read more
Woot offers numerous 2018-2020 MacBook Pros a...
Amazon-owned Woot has many open-box return MacBook Airs and MacBook Pros available today at prices starting at $879. Shipping is free for Prime members. Here’s what they have as of this post, and... Read more
Apple restocks refurbished 2020 13″ MacBook A...
Apple has restocked Certified Refurbished 2020 13″ MacBook Airs starting at only $849 and up to $200 off the cost of new Airs. Each MacBook features a new outer case, comes with a standard Apple one-... Read more
Apple restocks clearance 2019 13″ 2.4GHz MacB...
Apple has restocked Certified Refurbished 2019 13″ 2.4GHz 4-Core Touch Bar MacBook Pros starting at $1359 and up to $560 off original MSRP. Apple’s one-year warranty is included, shipping is free,... Read more
Apple restocks refurbished iPhone XR models s...
Apple has restocked Certified Refurbished, unlocked, iPhone XR models in the refurbished section of their online store starting at $539. Each iPhone comes with Apple’s standard one-year warranty,... Read more

Jobs Board

Cub Foods - *Apple* Valley - Now Hiring Par...
Cub Foods - Apple Valley - Now Hiring Part Time! United States of America, Minnesota, Apple Valley New Retail Post Date 6 days ago Requisition # 122305 Sign Up Read more
Director of *Apple* Enterprise Operations -...
## Description JOB SUMMARY: The Director of Apple Enterprise Operations is responsible for developing and managing the overall strategy, operation and management of Read more
Cub Foods - *Apple* Valley - Now Hiring Par...
Cub Foods - Apple Valley - Now Hiring Part Time! United States of America, Minnesota, Apple Valley New Retail Post Date 5 days ago Requisition # 122305 Sign Up Read more
Blue *Apple* Cafe Student Worker - Fall - P...
…to enhance your work experience. Student positions are available at the Blue Apple Cafe. Employee meal discount during working hours is provided. Duties include food Read more
Cub Foods - *Apple* Valley - Now Hiring Par...
Cub Foods - Apple Valley - Now Hiring Part Time! United States of America, Minnesota, Apple Valley New Retail Post Date 4 days ago Requisition # 122305 Sign Up Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.