Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Created September 30, 2011 09:42
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save igniteflow/1253276 to your computer and use it in GitHub Desktop.
Save igniteflow/1253276 to your computer and use it in GitHub Desktop.
A simple stopwatch implemented in Python
import datetime
class Timer(object):
"""A simple timer class"""
def __init__(self):
pass
def start(self):
"""Starts the timer"""
self.start = datetime.datetime.now()
return self.start
def stop(self, message="Total: "):
"""Stops the timer. Returns the time elapsed"""
self.stop = datetime.datetime.now()
return message + str(self.stop - self.start)
def now(self, message="Now: "):
"""Returns the current time with a message"""
return message + ": " + str(datetime.datetime.now())
def elapsed(self, message="Elapsed: "):
"""Time elapsed since start was called"""
return message + str(datetime.datetime.now() - self.start)
def split(self, message="Split started at: "):
"""Start a split timer"""
self.split_start = datetime.datetime.now()
return message + str(self.split_start)
def unsplit(self, message="Unsplit: "):
"""Stops a split. Returns the time elapsed since split was called"""
return message + str(datetime.datetime.now() - self.split_start)
@julianpraxis
Copy link

I started learning Python very recently. This code is useful to me both to understand how Python works and to use it as a practical timer. Thank you!

@hamadycisse
Copy link

Works well. Thank you

@pedrohgi
Copy link

Really nice!

@s0h1s2
Copy link

s0h1s2 commented Aug 10, 2018

Wow this help me a lot Thanks for You

@wildcard329
Copy link

Why the triple quotes instead of a hash?

@ClaytonSibanda
Copy link

its a doc string not a comment @wildcard329

@MelanX
Copy link

MelanX commented Apr 4, 2019

Am I allowed to use it in my code and publish on GitHub? (Because I can't find license)

@MelanX
Copy link

MelanX commented May 11, 2019

https://gist.github.com/MelanX/8952e70d52fb17097f8df097cbd96c79
I changed your code a bit and added a reset def. maybe it's better :) I don't know how to create a real PR here :D

@lonzoball2214
Copy link

What type of python is this used for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment