0

I wrote a program that read the time each second and we should choose the target time and when the system time equaled to target a time that program shows a notification but that program wasn't optimized because it works with memory very much.

that was like this:

import datetime

while(1):
    currentDT = datetime.datetime.now()
    if "00:00:00 PM" ==str(currentDT.strftime("%I:%M:%S %p")):
        alarm
        break
    else:
        continue

is anyone can customize that or suggest a better method?

Alirezaarabi
  • 348
  • 8
  • 25
  • 1
    In this program the seconds are important. – Alirezaarabi Apr 01 '20 at 16:30
  • "I wrote a program" Oh really? I didn't know that? That sounds interesting? Tell me more about it! Can you show me your code? Why is it using too much memory? Have you tried googling how to reduce the memory usage? – Gareth Ma Apr 01 '20 at 16:38
  • that was a simple code and I googled that but I couldn't find a good thing – Alirezaarabi Apr 01 '20 at 16:43
  • please please please even if you googled it just include it in your code everything helps omg add to your post please thank you edit it – Gareth Ma Apr 01 '20 at 16:43
  • ok I added the same code in my post and I found how to optimize but that wasn't good for me because the seconds are important for me – Alirezaarabi Apr 01 '20 at 16:47
  • The reason why your code is slow/using much memory is because you're constantly checking the date and calling the `datetime.datetime.now()`. The function is *really* expensive to call. Try to do is minimally, i.e. since one second of margin of error is acceptable, call it every 0.8 sec – Gareth Ma Apr 01 '20 at 16:54
  • I tried this way at past but this way using memory very much it's good but it's not working on that program because I used multi tread in that program – Alirezaarabi Apr 01 '20 at 16:59
  • Can you pos your "multi thread" program? – Gareth Ma Apr 01 '20 at 17:00
  • if you know a better way I'm happy to know that – Alirezaarabi Apr 01 '20 at 17:00
  • 1
    Does this answer your question? [Run certain code every n seconds](https://stackoverflow.com/questions/3393612/run-certain-code-every-n-seconds) – Gareth Ma Apr 01 '20 at 17:00
  • I can post my multi tread but it can't help because that works on UI and this part is in the back – Alirezaarabi Apr 01 '20 at 17:06
  • Does the link above answer your question? If not why not – Gareth Ma Apr 01 '20 at 17:06
  • My propose of "This program has multi tread" was this program use memory very much as when as it lunch – Alirezaarabi Apr 01 '20 at 17:07
  • Like I don't believe there's any other way to be honest. You can take my answer, or you can not. – Gareth Ma Apr 01 '20 at 17:08
  • Ok, your answer isn't wrong. actually, that is true but if there's a way to do that better, that can help me better – Alirezaarabi Apr 01 '20 at 17:11

1 Answers1

0

Instead how about you try to use time?

import time
while True:
    ...
    if ...:
        alarm
        break
    time.sleep(0.8)

Don't sleep the whole second, since there might be a slight delay and it sleeps for 1.001 seconds, which may cause you to miss the second mark. 0.8 seconds should work fine.

Gareth Ma
  • 707
  • 4
  • 10