-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathppc2_timing.py
108 lines (88 loc) · 3.6 KB
/
ppc2_timing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: utf-8 -*-
"""
Timing python code and psychopy using the handy ppc.timer() function.
It's used to identify bottlenecks in the script, especially the part
within the flip()-loop.
Jonas Kristoffer Lindeløv, 2013. Revised in 2015.
"""
import ppc
# -------------------------
# TIMING BASIC PYTHON STUFF
# Basic stuff is very fast!
# -------------------------
ppc.timer('pass') # should give something close to 0
ppc.timer('if 99 == 99: pass')
ppc.timer('if "a" == "a": pass')
ppc.timer('if "abracadabra" == "abracadabra": pass') # string length means nothing
ppc.timer('myVariable = 4 + 5 * 2 / 3')
# Putting stuff in __main__ and time them
three = 3
ppc.timer('4 + three', 'three') # using a variable is slower
# -------------------------
# TIMING PSYCHOPY STUFF
# * Initating stimuli is slow, > 10ms
# * Setting properties is medium. 0.1 - 5 ms (slower for text)
# * Drawing is fast
# -------------------------
from psychopy import core, visual
win = visual.Window()
stim = visual.TextStim(win)
clock = core.Clock()
# Checking core.Clock() timing:
clock.reset() # set clock to zero
core.wait(0.1) # wait a little
print '\'core.wait(0.1)\'', clock.getTime() # check the clock. It should be pretty accurate.
"""
TIMING EXERCISES:
1. How long does it take to create a win = visual.Window()?
2. How long does it take to reset a clock?
3. How long does it take to get current time of clock?
4. Test whether core.wait() is accurate, e.g. by setting it to 7 ms
OBS: core.wait() is a slow first-runner. Run once before doing the timing.
4. Create a TextStim.
How long does it take to do textStim.text = 'hi'
How long does it take to do textStim.text = 'hi there my dear old friend. I\'m very happy to see you again! We should meet more often'
5. Pro: Do a multi-line alteration of a GratingStim (rotation, color etc.)
and time it including the draw but excluding the flip.
6. Pro: How large a text can you put into visual.TextStim.text without loosing frames on your system?
"""
# SOLUTIONS #1-#3
ppc.timer('win = visual.Window()', 'visual')
ppc.timer('clock.reset()', 'clock')
ppc.timer('clock.getTime()', 'clock')
# 4 short vs. long text
stim = visual.TextStim(win)
ppc.timer('stim.text = "hi"', 'stim')
ppc.timer('stim.text = "hi there my dear old friend. I\'m very happy to see you again! We should meet more often"', 'stim')
# 6 milti-line
stim = visual.GratingStim(win)
script = """
stim.pos = [0, 1]
stim.ori = 45
stim.color = 'black'
stim.draw()
#win.flip()
duration = clock.getTime()
clock.reset()
"""
ppc.timer(script, 'stim, clock')
# You can time stuff in the actual experiment to get more ecologically valid timing
# However, they give identical results (as they should) so ppc.timer() is simpler.
# ppc.timer() might be too optimistic but close enough to be useful for identifying
# bottlenecks.
stim = visual.TextStim(win)
clock = core.Clock()
def testUsingClock(code):
times = [] # we'll fill this list with individual execution times
for trial in range(1000):
clock.reset()
eval(code)
times += [clock.getTime()]
return 1000 * sum(times) / len(times)
def compare(code):
# Calculate mean execution time and compare to "timer" function
ppc.timer(code, setup='core, stim')
print '\nclock baseline:', testUsingClock(code), 'ms' # negligeble, therefore not used
compare('stim.pos = (1,1)')
compare('core.wait(0.0017)') # should be close to 1.7 ms
compare('stim.text = "Setting a long text is slow"')