Python Material Installation Dokumentation PythonKara Grundlagen Namensräume Funktionen Testen Strings Dictionary Listen Zeitmessungen Zufall Bits+Bytes Module Sockets xml serielle Schnittstelle Python in HTML Turtle xturtle Tkinter OOP Threads Zusicherungen exe Patterns GnuPlot Goto MySQL CGI Dateien Exceptions Grafik Mathematik Fischertechnik Unicode funktional Iterator Sound C Debugger regex Pfade Docstrings Django Bluetooth format Bytecode signal
Pfad: Startseite / Fächer / Informatik / Python / Zeitmessungen
Autor: mk
13.10.2011 11:33
2130
Zeitmessungen

Auszug aus der Python-Hilfe zum Modul 'time':

clock( )

On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the
very definition of the meaning of "processor time", depends on that of the C function of the same name, but in any case,
this is the function to use for benchmarking Python or timing algorithms.
On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point
number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

Beispiel für eine Zeitmessung

# -*- coding: iso-8859-1 -*-
# Autor: Klaus Merkert, Datum: 29.5.08

from time import *

print 'Primzahltester (einfach)\n'

n = input('n = ')

t1 = clock() # ----------------------------------- Anfangszeit in s
t = 0
i = 1
while i <= n:
    if n%i == 0:
        t = t+1
    i = i+1
t2 = clock() # ----------------------------------- Endzeit in s
dt = t2 - t1 # ---------- Zeitdifferenz = Endzeit - Anfangszeit

if t == 2:
    print '\n'+str(n)+' ist eine Primzahl.'
else:
    print '\n'+str(n)+' ist keine Primzahl.'
print '\nRechenzeit: '+str(dt)+'s\n'

time0.py

Zusatz

Es hat sich gezeigt, dass entgegen obiger Angaben unter Linux time.time eine wesentlich bessere Auflösung hat als time.clock. Außerdem scheint timeit eher für Messungen geeignet zu sein, da einerseits die garbage collection für die Messung abgeschaltet wird, andererseits die Messung oft wiederholt wird. Die drei Parameter von Timer bedeuten: zu untersuchender Code (hier a*b), dafür notwendiger setup, zu verwendende 'Stoppuhr'.

import timeit

t = timeit.Timer('a*b','a = 17;b = 42',time.time)
zeit = t.timeit(number = 1000000)
print(zeit)

Links

Valid XHTML 1.0!