# Psychtoolbox doesn't follow timings

**URL:** <https://psychtoolbox.discourse.group/t/psychtoolbox-doesnt-follow-timings/4123>\
**Category:** Programming Help\
**Created:** [November 17, 2021, 10:00am UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-doesnt-follow-timings/4123 "2021-11-17T10:00:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michalwe](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@Michalwe](https://psychtoolbox.discourse.group/u/Michalwe)\
**Post date:** [November 17, 2021, 10:00am UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-doesnt-follow-timings/4123/1 "2021-11-17T10:00:13Z")

</div>

Hi,  
I’m working on i7 HP, with Matlab 2021b and Pshyctoolbox 3.0.18 -m beta.

I’m trying to present a stimuli while recording keyboard activity.  
I’m running the following code - which works fine on other computers:

```
imageTexture = Screen('MakeTexture', window, Image);
Screen('DrawTexture', window, imageTexture);
Screen('Flip', window);   
t0=clock;
j=0;
while etime(clock,t0)<0.25
    j=j+1;
    [Key(i,j),secs(i,j),KeyCode,deltaSecs]=KbCheck;
end

```

But the stimuli end up being presented for much less then 0.25 seconds, although when I check the timings it says that it was presented for 0.25 sec.

Thank you,

---

<div class="post-metadata">

**Author:** ![dcnieho](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dcnieho](https://psychtoolbox.discourse.group/u/dcnieho)\
**Post date:** [November 17, 2021, 10:36am UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-doesnt-follow-timings/4123/2 "2021-11-17T10:36:58Z")

</div>

Solutions ordered in terms of both how much work they are and how  
recommended they are:

1. Use GetSecs not the matlab time functions, the latter may have  
pretty low resolution depending on clock hardware
2. move to using KbQueues (see KbQueueDemo), along with using  
Screen(‘Flip’)'s when parameter for presentation timing, as  
recommended in the PTBTutorial-ECVP2013.pdf in the PsychDocumentation  
folder

---

<div class="post-metadata">

**Author:** ![Michalwe](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@Michalwe](https://psychtoolbox.discourse.group/u/Michalwe)\
**Post date:** [November 17, 2021, 11:57am UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-doesnt-follow-timings/4123/3 "2021-11-17T11:57:47Z")

</div>

Amazing! the first solution worked! Thank you

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [November 17, 2021, 6:00pm UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-doesnt-follow-timings/4123/4 "2021-11-17T18:00:00Z")

</div>

To emphasize Diedericks comment:

If you don’t use the timestamps and target times of the `Screen('Flip')` command, e.g., `t0 = Screen('Flip', window)` then your t0 may have no relationship to true stimulus presentation time at all, there could be arbitrary errors. We have many demos and help in the cited PDF file on how to do timing right, instead of fooling oneself or suffering from way less precision than possible.

-mario
