8

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)
3
  • 1
    Bear in mind that whatever operations you choose, the version of Python that's used to run the benchmark could be a significant factor in the timings you get. Commented Nov 30, 2011 at 13:55
  • My intention is to freeze it with cx_Freeze and then run it in Windows. If I use always the same frozen pack the timings should proceed, am I right? Commented Nov 30, 2011 at 14:00
  • 1
    If everything you need to run the benchmark is inside the frozen package, then yes. Commented Nov 30, 2011 at 14:01

6 Answers 6

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @werewindle. I'm especially interested in functions which push the memory, but CPU can also be used. Could you give me some example or some link where I can learn more about this especific calculations?
you can use numpy to generate huge random square matrix (like 10 000x10 000) and then calculate inverse matrix. This will effectively stress CPU and memory bus.
I decided to create a group of 10 tests in a row. One of them reading all the words of a huge file and encoding it with hashlib, another iterating and calculating integers with a big range. And one with your suggestion, using numpy to create random matrices, calculating them, multiplying them and using all the functions I could. And it worked pretty well, the PC spent some seconds on it. I'm still thinking on other tests to complete the set, but your suggestions are pretty good, Thank you. (After a lot of tests my PC got slower than a slug so I guess it really achieves the goal).
5

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

Comments

2

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')

1 Comment

Thank you for the answer. I'm expecting something a little more rough. And maybe more then just one test, you know.
1

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.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.