GetSecs versus matlab7 tic and toc

Is there any advantage to using getsecs over matlab's buit-in tic & toc
commands in matlab7. I know that in matlab 6 and earlier tic & toc had
very poor resolution 10 ms on my windows machines, but I've noticed
that in matlab7, toc seems to have extremely high resolution (sub
nanosecond) and it takes less time to execute than getsecs (although
they're both fast - 2us versus 4us). This seems true in both windows
and OS-X in my limited testing.

So is there any advantage to using getsecs?
dear mark

thanks for doing the testing. yes, it seems that tic toc are probably
using the same time base.

the time base used by GetSecs is a counter that runs inside the
PowerPC chip, and stops for nothing. all the other Mac timebases lose
time (miss interrupts) when the processor priority is high.

best

denis

On Jan 9, 2006, at 6:09 PM, msmith77777 wrote:

> Dennis,
>
> Thanks for your comments.
>
> My comments before were about resolution (precision) not accuracy, but
> I guess accuracy is more important. The (minor) problem for
> determining accuracy is: what should/could we use as a gold standard?
> If we use getsecs (which is reasonable) then here is a simple test:
>
> priority(2);
> N=1e6; t2=zeros(N,2); t1a=getsecs; tic; t1b=getsecs;
> for k=1:N, t2(k,1)=getsecs; t2(k,2)=toc; end;
> priority(0);
>
> t1b-t1a
> min(t2(:,2)-[t2(:,1)-t1b])
> max(t2(1:end-1,2)-[t2(2:end,1)-t1a])
>
> This code make one million time measurements with toc and one million
> two with getsecs (and runs in about 7 seconds on my PC). If toc and
> getsecs are measuring the same thing then for each k, t2(k,2) should
> be bounded below t2(k+1,1)-t1b, and t2(k,2) should be bounded above by
> t2(k,1)-t1a. So min(t2(:,2)-[t2(:,1)-t1b]) should be above zero and
> max(t2(1:end-1,2)-[t2(2:end,1)-t1a] should be below zero.
>
> I find this to be consistently true. So this tells us that toc is
> accurate to getsecs within [t1b-t1a] + [t2(k,1)-t2(k-1,1)]. This
> range is about 27 microseconds for most iterations in my machine.
>
> Since Matlab takes more time to execute statemets outside the for loop
> than inside (and the first 50 or so loop iterations are slower that
> than the rest) we can get a tighter bound for the accuracy of tic to
> getsecs by comparing measurements made within the loop to each other
> than by comparing loop measurements to initial measurements (like
> t1a=getsecs).
>
>
> so if after running the above code, we say:
> t1a = t2(100,1);
> t1b = t2(101,1);
> t2 = [t2(102:end, 1), t2(102:end,2) - t2(100,2)];
>
> then
>
> min(t2(:,2)-[t2(:,1)-t1b])
> max(t2(1:end-1,2)-[t2(2:end,1)-t1a])
>
> min([t1b-t1a] + [t2(2:end,1)-t2(1:end-1,1)])
>
> we find that t2(k,2) always stay with it's prescribed bounds, but
> these new bounds are tighter with a range of about 14 microseconds on
> most iterations. I don't know how to get tighter bounds than that,
> other than running this code on a faster computer.
>
> All this may be overkill for a simple question, but it does seem that
> toc in matlab 7 is pretty darn accurate, and probably is as accurate
> as getsecs. However, I haven't been able the find any info from
> Mathworks mentioning that tic, toc, clock etc have been rewritten /
> improved in matlab 7.
>
> The execution time comment in my previous post was just an aside, but
> if you're trying to write the fastest code possible, the faster
> execution time of toc versus getsecs suggests that it may be better to
> use toc.
>
> Thanks,
> Maurice
>
>
>
>
> --- In psychtoolbox@yahoogroups.com, Denis Pelli <denis.pelli@n...>
> wrote:
>>
>> dear msmith
>>
>> i wrote GetSecs for the OS9 Psychtoolbox, but i don't think it's
>> changed in the OSX version. I have not tested tic & toc in MATLAB 7.
>> from what you say, it may be fine to switch, but the test you
>> reported is not definitive.
>>
>> i suggest you measure a duration with both and see if the two answers
>> agree, except for latencies. if so, then it's fine to use tic and
>> toc.
>>
>> there's a handy feature in the OS9 Psychtoolbox called "time
>> stamping" that records the time of the most recent blanking pulse.
>> This uses GetSecs, so in that context you may prefer to use GetSecs
>> to compare that time (not duration) with that of other events.
>>
>> best
>>
>> denis
>>
>> On Jan 7, 2006, at 1:12 AM, msmith77777 wrote:
>>
>>> Is there any advantage to using getsecs over matlab's buit-in tic &
>>> toc
>>> commands in matlab7. I know that in matlab 6 and earlier tic & toc
>>> had
>>> very poor resolution 10 ms on my windows machines, but I've noticed
>>> that in matlab7, toc seems to have extremely high resolution (sub
>>> nanosecond) and it takes less time to execute than getsecs (although
>>> they're both fast - 2us versus 4us). This seems true in both windows
>>> and OS-X in my limited testing.
>>>
>>> So is there any advantage to using getsecs?
>>>
>>>
>>>
>>>
>>>
>>>
>>> Post your message to: psychtoolbox@yahoogroups.com
>>> Please indicate OS9, OSX, or WIN version, and include your full
>>> name.
>>> Denis Pelli, David Brainard, and Allen Ingling.
>>> http://psychtoolbox.org
>>>
>>> Yahoo! Groups Links
>>>
>>>
>>>
>>>
>>>
>>>
>>
>
>
>
>
>
>
>
> Post your message to: psychtoolbox@yahoogroups.com
> Please indicate OS9, OSX, or WIN version, and include your full name.
> Denis Pelli, David Brainard, and Allen Ingling.
> http://psychtoolbox.org
>
> Yahoo! Groups Links
>
>
>
>
>
>

Denis Pelli
Professor of Psychology and Neural Science
http://psych.nyu.edu/pelli/
Denis,

How accurate is the CPU clock? I read (in some pychtoolbox documentation) that the
vertical refresh rates of video cards change as they heat up during normal use - I assume
this means that some clock onboard this card speeds up or slows down as it's heated. Is
this also true of the CPU clock? How about clocks on A/D boards?

Thanks,
Maurice


--- In psychtoolbox@yahoogroups.com, Denis Pelli <denis.pelli@n...> wrote:
>
> dear mark
>
> thanks for doing the testing. yes, it seems that tic toc are probably
> using the same time base.
>
> the time base used by GetSecs is a counter that runs inside the
> PowerPC chip, and stops for nothing. all the other Mac timebases lose
> time (miss interrupts) when the processor priority is high.
>
> best
>
> denis
>
> On Jan 9, 2006, at 6:09 PM, msmith77777 wrote:
>
> > Dennis,
> >
> > Thanks for your comments.
> >
> > My comments before were about resolution (precision) not accuracy, but
> > I guess accuracy is more important. The (minor) problem for
> > determining accuracy is: what should/could we use as a gold standard?
> > If we use getsecs (which is reasonable) then here is a simple test:
> >
> > priority(2);
> > N=1e6; t2=zeros(N,2); t1a=getsecs; tic; t1b=getsecs;
> > for k=1:N, t2(k,1)=getsecs; t2(k,2)=toc; end;
> > priority(0);
> >
> > t1b-t1a
> > min(t2(:,2)-[t2(:,1)-t1b])
> > max(t2(1:end-1,2)-[t2(2:end,1)-t1a])
> >
> > This code make one million time measurements with toc and one million
> > two with getsecs (and runs in about 7 seconds on my PC). If toc and
> > getsecs are measuring the same thing then for each k, t2(k,2) should
> > be bounded below t2(k+1,1)-t1b, and t2(k,2) should be bounded above by
> > t2(k,1)-t1a. So min(t2(:,2)-[t2(:,1)-t1b]) should be above zero and
> > max(t2(1:end-1,2)-[t2(2:end,1)-t1a] should be below zero.
> >
> > I find this to be consistently true. So this tells us that toc is
> > accurate to getsecs within [t1b-t1a] + [t2(k,1)-t2(k-1,1)]. This
> > range is about 27 microseconds for most iterations in my machine.
> >
> > Since Matlab takes more time to execute statemets outside the for loop
> > than inside (and the first 50 or so loop iterations are slower that
> > than the rest) we can get a tighter bound for the accuracy of tic to
> > getsecs by comparing measurements made within the loop to each other
> > than by comparing loop measurements to initial measurements (like
> > t1a=getsecs).
> >
> >
> > so if after running the above code, we say:
> > t1a = t2(100,1);
> > t1b = t2(101,1);
> > t2 = [t2(102:end, 1), t2(102:end,2) - t2(100,2)];
> >
> > then
> >
> > min(t2(:,2)-[t2(:,1)-t1b])
> > max(t2(1:end-1,2)-[t2(2:end,1)-t1a])
> >
> > min([t1b-t1a] + [t2(2:end,1)-t2(1:end-1,1)])
> >
> > we find that t2(k,2) always stay with it's prescribed bounds, but
> > these new bounds are tighter with a range of about 14 microseconds on
> > most iterations. I don't know how to get tighter bounds than that,
> > other than running this code on a faster computer.
> >
> > All this may be overkill for a simple question, but it does seem that
> > toc in matlab 7 is pretty darn accurate, and probably is as accurate
> > as getsecs. However, I haven't been able the find any info from
> > Mathworks mentioning that tic, toc, clock etc have been rewritten /
> > improved in matlab 7.
> >
> > The execution time comment in my previous post was just an aside, but
> > if you're trying to write the fastest code possible, the faster
> > execution time of toc versus getsecs suggests that it may be better to
> > use toc.
> >
> > Thanks,
> > Maurice
> >
> >
> >
> >
> > --- In psychtoolbox@yahoogroups.com, Denis Pelli <denis.pelli@n...>
> > wrote:
> >>
> >> dear msmith
> >>
> >> i wrote GetSecs for the OS9 Psychtoolbox, but i don't think it's
> >> changed in the OSX version. I have not tested tic & toc in MATLAB 7.
> >> from what you say, it may be fine to switch, but the test you
> >> reported is not definitive.
> >>
> >> i suggest you measure a duration with both and see if the two answers
> >> agree, except for latencies. if so, then it's fine to use tic and
> >> toc.
> >>
> >> there's a handy feature in the OS9 Psychtoolbox called "time
> >> stamping" that records the time of the most recent blanking pulse.
> >> This uses GetSecs, so in that context you may prefer to use GetSecs
> >> to compare that time (not duration) with that of other events.
> >>
> >> best
> >>
> >> denis
> >>
> >> On Jan 7, 2006, at 1:12 AM, msmith77777 wrote:
> >>
> >>> Is there any advantage to using getsecs over matlab's buit-in tic &
> >>> toc
> >>> commands in matlab7. I know that in matlab 6 and earlier tic & toc
> >>> had
> >>> very poor resolution 10 ms on my windows machines, but I've noticed
> >>> that in matlab7, toc seems to have extremely high resolution (sub
> >>> nanosecond) and it takes less time to execute than getsecs (although
> >>> they're both fast - 2us versus 4us). This seems true in both windows
> >>> and OS-X in my limited testing.
> >>>
> >>> So is there any advantage to using getsecs?
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>> Post your message to: psychtoolbox@yahoogroups.com
> >>> Please indicate OS9, OSX, or WIN version, and include your full
> >>> name.
> >>> Denis Pelli, David Brainard, and Allen Ingling.
> >>> http://psychtoolbox.org
> >>>
> >>> Yahoo! Groups Links
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>
> >
> >
> >
> >
> >
> >
> >
> > Post your message to: psychtoolbox@yahoogroups.com
> > Please indicate OS9, OSX, or WIN version, and include your full name.
> > Denis Pelli, David Brainard, and Allen Ingling.
> > http://psychtoolbox.org
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
> >
> >
>
> Denis Pelli
> Professor of Psychology and Neural Science
> http://psych.nyu.edu/pelli/
>
dear maurice

i don't know. i believe you're right about the existence of the
temperature effects, but i have never had a good enough time standard
against which to measure them.

best

denis

On Jan 10, 2006, at 4:35 AM, msmith77777 wrote:

> Denis,
>
> How accurate is the CPU clock? I read (in some pychtoolbox
> documentation) that the
> vertical refresh rates of video cards change as they heat up during
> normal use - I assume
> this means that some clock onboard this card speeds up or slows
> down as it's heated. Is
> this also true of the CPU clock? How about clocks on A/D boards?
>
> Thanks,
> Maurice
>
>
> --- In psychtoolbox@yahoogroups.com, Denis Pelli <denis.pelli@n...>
> wrote:
>>
>> dear mark
>>
>> thanks for doing the testing. yes, it seems that tic toc are probably
>> using the same time base.
>>
>> the time base used by GetSecs is a counter that runs inside the
>> PowerPC chip, and stops for nothing. all the other Mac timebases lose
>> time (miss interrupts) when the processor priority is high.
>>
>> best
>>
>> denis
>>
>> On Jan 9, 2006, at 6:09 PM, msmith77777 wrote:
>>
>>> Dennis,
>>>
>>> Thanks for your comments.
>>>
>>> My comments before were about resolution (precision) not
>>> accuracy, but
>>> I guess accuracy is more important. The (minor) problem for
>>> determining accuracy is: what should/could we use as a gold
>>> standard?
>>> If we use getsecs (which is reasonable) then here is a simple test:
>>>
>>> priority(2);
>>> N=1e6; t2=zeros(N,2); t1a=getsecs; tic; t1b=getsecs;
>>> for k=1:N, t2(k,1)=getsecs; t2(k,2)=toc; end;
>>> priority(0);
>>>
>>> t1b-t1a
>>> min(t2(:,2)-[t2(:,1)-t1b])
>>> max(t2(1:end-1,2)-[t2(2:end,1)-t1a])
>>>
>>> This code make one million time measurements with toc and one
>>> million
>>> two with getsecs (and runs in about 7 seconds on my PC). If toc and
>>> getsecs are measuring the same thing then for each k, t2(k,2) should
>>> be bounded below t2(k+1,1)-t1b, and t2(k,2) should be bounded
>>> above by
>>> t2(k,1)-t1a. So min(t2(:,2)-[t2(:,1)-t1b]) should be above zero and
>>> max(t2(1:end-1,2)-[t2(2:end,1)-t1a] should be below zero.
>>>
>>> I find this to be consistently true. So this tells us that toc is
>>> accurate to getsecs within [t1b-t1a] + [t2(k,1)-t2(k-1,1)]. This
>>> range is about 27 microseconds for most iterations in my machine.
>>>
>>> Since Matlab takes more time to execute statemets outside the for
>>> loop
>>> than inside (and the first 50 or so loop iterations are slower that
>>> than the rest) we can get a tighter bound for the accuracy of tic to
>>> getsecs by comparing measurements made within the loop to each other
>>> than by comparing loop measurements to initial measurements (like
>>> t1a=getsecs).
>>>
>>>
>>> so if after running the above code, we say:
>>> t1a = t2(100,1);
>>> t1b = t2(101,1);
>>> t2 = [t2(102:end, 1), t2(102:end,2) - t2(100,2)];
>>>
>>> then
>>>
>>> min(t2(:,2)-[t2(:,1)-t1b])
>>> max(t2(1:end-1,2)-[t2(2:end,1)-t1a])
>>>
>>> min([t1b-t1a] + [t2(2:end,1)-t2(1:end-1,1)])
>>>
>>> we find that t2(k,2) always stay with it's prescribed bounds, but
>>> these new bounds are tighter with a range of about 14
>>> microseconds on
>>> most iterations. I don't know how to get tighter bounds than that,
>>> other than running this code on a faster computer.
>>>
>>> All this may be overkill for a simple question, but it does seem
>>> that
>>> toc in matlab 7 is pretty darn accurate, and probably is as accurate
>>> as getsecs. However, I haven't been able the find any info from
>>> Mathworks mentioning that tic, toc, clock etc have been rewritten /
>>> improved in matlab 7.
>>>
>>> The execution time comment in my previous post was just an aside,
>>> but
>>> if you're trying to write the fastest code possible, the faster
>>> execution time of toc versus getsecs suggests that it may be
>>> better to
>>> use toc.
>>>
>>> Thanks,
>>> Maurice
>>>
>>>
>>>
>>>
>>> --- In psychtoolbox@yahoogroups.com, Denis Pelli <denis.pelli@n...>
>>> wrote:
>>>>
>>>> dear msmith
>>>>
>>>> i wrote GetSecs for the OS9 Psychtoolbox, but i don't think it's
>>>> changed in the OSX version. I have not tested tic & toc in
>>>> MATLAB 7.
>>>> from what you say, it may be fine to switch, but the test you
>>>> reported is not definitive.
>>>>
>>>> i suggest you measure a duration with both and see if the two
>>>> answers
>>>> agree, except for latencies. if so, then it's fine to use tic and
>>>> toc.
>>>>
>>>> there's a handy feature in the OS9 Psychtoolbox called "time
>>>> stamping" that records the time of the most recent blanking pulse.
>>>> This uses GetSecs, so in that context you may prefer to use GetSecs
>>>> to compare that time (not duration) with that of other events.
>>>>
>>>> best
>>>>
>>>> denis
>>>>
>>>> On Jan 7, 2006, at 1:12 AM, msmith77777 wrote:
>>>>
>>>>> Is there any advantage to using getsecs over matlab's buit-in
>>>>> tic &
>>>>> toc
>>>>> commands in matlab7. I know that in matlab 6 and earlier tic &
>>>>> toc
>>>>> had
>>>>> very poor resolution 10 ms on my windows machines, but I've
>>>>> noticed
>>>>> that in matlab7, toc seems to have extremely high resolution (sub
>>>>> nanosecond) and it takes less time to execute than getsecs
>>>>> (although
>>>>> they're both fast - 2us versus 4us). This seems true in both
>>>>> windows
>>>>> and OS-X in my limited testing.
>>>>>
>>>>> So is there any advantage to using getsecs?
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Post your message to: psychtoolbox@yahoogroups.com
>>>>> Please indicate OS9, OSX, or WIN version, and include your full
>>>>> name.
>>>>> Denis Pelli, David Brainard, and Allen Ingling.
>>>>> http://psychtoolbox.org
>>>>>
>>>>> Yahoo! Groups Links
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> Post your message to: psychtoolbox@yahoogroups.com
>>> Please indicate OS9, OSX, or WIN version, and include your full
>>> name.
>>> Denis Pelli, David Brainard, and Allen Ingling.
>>> http://psychtoolbox.org
>>>
>>> Yahoo! Groups Links
>>>
>>>
>>>
>>>
>>>
>>>
>>
>> Denis Pelli
>> Professor of Psychology and Neural Science
>> http://psych.nyu.edu/pelli/
>>
>
>
>
>
>
>
>
>
>
> Post your message to: psychtoolbox@yahoogroups.com
> Please indicate OS9, OSX, or WIN version, and include your full name.
> Denis Pelli, David Brainard, and Allen Ingling.
> http://psychtoolbox.org
>
> Yahoo! Groups Links
>
>
>
>
>
>

Denis Pelli
Professor of Psychology and Neural Science
http://psych.nyu.edu/pelli/