-1

the problem is that when i run my script it takes longer than the expected time 1 second before it says the next command. i think this has something to do with the speech command. what can i do to optimize this?

edit: link to the sppech module https://pypi.python.org/pypi/speech/0.5.2

edit2: per request i measured the sleep time only using datetime. 2016-06-29 18:39:42.953000 2016-06-29 18:39:43.954000 i found that it was pretty accurate

edit3: i tried the build in import win32com.client and it didnt work either

import speech
import time
import os 

def exercise1():
    speech.say("exercise1")
    time.sleep(0.5)
    for n in range(0, rep*2):
        speech.say("1")
        t   ime.sleep(1)
        speech.say("2")
        time.sleep(1)
        speech.say("3")
        time.sleep(1)
        speech.say("switch")
rasmus393
  • 15
  • 5
  • 3
    capture the system time before/after the say() calls. you'll probably find taht sleep is pretty much exactly 1 second, and the say() call is the one with the variable duration. – Marc B Jun 29 '16 at 16:35
  • can you hint me how to do that and yeah i think you are correct edit: i got this: 2016-06-29 18:39:42.953000 2016-06-29 18:39:43.954000 so almost perfectly 1 second how do i go about fixing the speech then? – rasmus393 Jun 29 '16 at 16:36

2 Answers2

0

Refer the post here How accurate is python's time.sleep()?

It says:

"The accuracy of the time.sleep function depends on the accuracy of your underlying OS's sleep accuracy. For non-realtime OS's like a stock Windows the smallest interval you can sleep for is about 10-13ms. I have seen accurate sleeps within several milliseconds of that time when above the minimum 10-13ms."

Community
  • 1
  • 1
Arun Ghosh
  • 7,634
  • 1
  • 26
  • 38
  • because of the other guy recomendation i did try to measusre the sleep time to find that it is pretty accurate 1 second – rasmus393 Jun 29 '16 at 16:41
0

As you say in the comments, sleep(1) is fairly accurately 1s.

What you want to do to make each part take 1s, is time the "say" call, and then wait the remaining time to fill out the second. Something like this:

start = time.time()
speech.say("whatever")
end = time.time()
sleep(1 - (end - start)) # Wait however long will bring the time up to 1 second total
mbrig
  • 929
  • 12
  • 16
  • ty i havent thought about that actually it still isnt going to match but ty i think this is the best its gonna be – rasmus393 Jun 29 '16 at 19:30
  • @rasmus393 If an answer was helpful or solved your problem, I'd encourage you to accept it (not necessarily mine!) – mbrig Jun 30 '16 at 02:59
  • is it fine it is just helpfull or does it have to solve my problem. not being cheeky – rasmus393 Jun 30 '16 at 10:03
  • because i tried doing this `def exercise1(): speaker.Speak("exercise1") time.sleep(0.5) for n in range(0, rep*2): start = time.time() speech.say("1") end = time.time() time.sleep(1 - (start - end)) start = time.time() speech.say("2")` and now it takes even longer i guess because there is more to prosses – rasmus393 Jun 30 '16 at 10:10
  • @rasmus393 ah, I had a typo, it should actually be `end - start`, that explains why it's taking longer – mbrig Jul 01 '16 at 03:30
  • now I have tried this 'start = time.time() speech.say("1") end = time.time() time.sleep(1 - (end - start)) start = time.time() speech.say("2") end = time.time() time.sleep(1 - (end - start))' and it just stoops like it is sleeping forever – rasmus393 Jul 01 '16 at 14:41