jonathan.hepp
jonathan.hepp

Reputation: 1643

Test computer processing speed with a simple Python script

I want to make a simple script just to test the time that the computer takes to execute it. I already built it with PyQt and made a kinda loop using QTimer. Now i need the "make busy" part. What kind of commands can I use just to make the computer work a little so I can get the time it takes and compare with other computers?

Here is my code so you can understand better:

self.Tempo = QtCore.QTimer(None)
self.Cron = QtCore.QTime(0,0,0,0)

def begin():
    self.Cron.start()
    self.Tempo.singleShot(999, update)       
def update():
    if self.lcdNumber.value() == 10:
        finish()                
    else:
        self.lcdNumber.display(self.lcdNumber.value()+1)
        #Here I want to make some processing stuff            
        self.Tempo.singleShot(999, update)
def finish():
    print("end")
    took = self.Cron.elapsed() / 1000
    print("took: {0} seconds" .format(str(took)))
    self.lcdNumber_2.display(took)

Upvotes: 8

Views: 28113

Answers (6)

Albi93
Albi93

Reputation: 55

You can benchmark CPU and/or RAM by implementing:

  • Vector addition
  • Matrix Vector Multiply
  • Matrix Multiply
  • Breadth-First Search (BFS)
  • Needleman Wunsch
  • Multilayer Perceptron

And a few others memory/CPU efficient algorithms.

Upvotes: 0

Parth Shah
Parth Shah

Reputation: 13

I found this useful for comparing the speeds:

import time

t=[]

for j in range(5):
    l = []

    pTime = time.time()

    for i in range(10000000):
        l.append(i)

    print('test took: ' + str(time.time() - pTime) + ' units time')
    t.append(time.time() - pTime)

s = 0
for i in t:
    s = s + i

print('average time = ' + str(s/len(t)))

The output for my pc being:

test took: 2.268193244934082 units time
test took: 2.0030276775360107 units time
test took: 1.7283973693847656 units time
test took: 1.7019169330596924 units time
test took: 1.8418810367584229 units time
average time = 1.9354074954986573

Upvotes: 0

Jeremy Whitcher
Jeremy Whitcher

Reputation: 721

Here's what I used to achieve a similar goal.

from multiprocessing import Pool, cpu_count
from datetime import datetime

def stress_test(args):
    cpu, value = args
    start_time = datetime.now()
    for i in range(value):
        value = value * i
    print(f"cpu: {cpu} time: {datetime.now() - start_time}")

if __name__ == '__main__':
    start_time = datetime.now()
    cpu_count = cpu_count()
    with Pool(cpu_count) as mp_pool:
        mp_pool.map(stress_test, [(cpu, 100000000) for cpu in range(cpu_count)])
    print(f"total: {datetime.now() - start_time}")

Result:

cpu: 5 time: 0:00:10.336081
cpu: 4 time: 0:00:10.372854
cpu: 3 time: 0:00:10.381920
cpu: 1 time: 0:00:10.492286
cpu: 7 time: 0:00:10.384343
cpu: 2 time: 0:00:10.570987
cpu: 6 time: 0:00:10.563981
cpu: 0 time: 0:00:10.921783
total: 0:00:12.450075

Upvotes: 5

causa prima
causa prima

Reputation: 1712

Instead of just appending some element to a list, you could add strings. String concatenation is more costy the bigger the strings get, which should test your memory performance, I guess.

test = "a test string"
for i in range(your_value):        # it takes forever, so choose this value wisely!
    if len(test) < 200000000:      # somewhere above this limit I get errors
        test += test
    else:
        test = test[0:len(test)/2] # if the string gets too long just cut it.

Upvotes: 1

werewindle
werewindle

Reputation: 3029

You can do any complex calculation problem in a loop:

  • Calculate factorial for some big number (easy to implement)
  • Calculate chain SHA1 hash 100 000 times (very easy to implement)
  • Invert big matrix (no so easy to implement)
  • ...
  • etc.

Some of those problems use CPU (factorial, SHA1), some others - CPU and memory (matrix invert). So first you need to decide, which part of computer you want to benchmark.

Upvotes: 6

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236004

Usually you can achieve that with a loop that does some simple work, something like this:

lst = []
for i in range(1000000):
    lst.append('x')

Upvotes: 2

Related Questions