1

I have written a small game about knight's tour problem in Python.

When I finished the algorithm part of the game, I found it run about 0.01 second to find a successful path of a 8*8 chess board. But when I run it on the computer of my office, it cost over 10 seconds to find the same path. Then I tried it on three other computers, the result is about 0.005, 6 and 8 seconds.

Why the execution speed of the same code has a huge difference on the five computers? The result is 0.005, 0.010, 6, 8 and 10 seconds. We can see the difference can be over 1000 times. The hardware of computers whose speeds are 6s and 8s are similar or better than 0.01's. And if the hardware affects the speed, it can't be that much -- about 1000 times.


I have corrected my code, the first edition has a mistake. I am using Python 3.6. And the test has been changed to 8*8 size, I'm sorry that I misremembered.


The following is the code.

import sys
import time

def init_path(size):
    allow = []
    for i in range(size):
        for j in range(size):
            allow.append([i, j])
    return allow

def get_next_choice(step, hist, raws, allow):
    num = 0
    for raw in raws:
        nextstep = [raw[i]+step[i] for i in range(2)]
        if nextstep in allow and nextstep not in hist:
            num += 1
    return num

def search_next(size, pos, history, allow):
    nextsteps = {}
    raws = [[1,2], [1,-2], [2,1], [2,-1], [-1,2], [-1,-2], [-2,1], [-2,-1]]
    if len(history) == size*size:
        return True
    for raw in raws:
        nextstep = [raw[i]+pos[i] for i in range(2)]
        if nextstep in allow and nextstep not in history:
            next_choice = get_next_choice(nextstep, history, raws, allow)
            nextsteps[next_choice] = nextstep
    sorted(nextsteps.items())

    for nextstep in nextsteps.values():
        history.append(nextstep)
        back = search_next(size, nextstep, history, allow)
        if back:
            return True
        else:
            history.pop()
    else:
        return False

def search_path(size, history):
    allow = init_path(size)
    position = history[-1]
    back = search_next(size, position, history, allow)
    if back:
        return history
    else:
        return False

atime = time.time()
path = search_path(8, [[0,0]])
btime = time.time()
print(btime - atime)
false
  • 10,264
  • 13
  • 101
  • 209
  • Sometimes path is False, sometimes it is a list of cells... – Reblochon Masque Dec 02 '17 at 09:31
  • 3
    Same python? Same ram? Same processor? Same backgroundactivity? – Patrick Artner Dec 02 '17 at 09:31
  • @ReblochonMasque I copy/pasted the code into pyfiddle 3.6 - no errors after fixing [0,0] to [[0,0]]? – Patrick Artner Dec 02 '17 at 09:33
  • @PatrickArtner I just pasted this code into Thonny (Python 3.6) and runtime error 'TypeError: int object is not subscriptable'. OK - I also made that fix: `search_path(6, [0,0])` -> `search_path(6, [[0,0]])` and output is `0.125 ...` – quamrana Dec 02 '17 at 09:37
  • @PatrickArtner: Yes – quamrana Dec 02 '17 at 09:40
  • I am sorry and I have corrected my code. I know that the hardware affects the speed, but I can't understand why it can't be that much -- about 1000 times. – yuanyuanzijin Dec 02 '17 at 11:06
  • All reasons are not connected to the hardware. Performance testing is done by giving equal load to all hardware. Old computers maybe got: `low CAS latency(RAM)`, wide processor frequency(8 core slower than 2 core on single core processes(fill_core(overload) >> switch to empty) ), and more and more. This means that all hardware components of your software must be adjusted according to the operating feature. Expensive is not always good, the good one is best suited to your work. **I strongly advise you to read the processor, memory and bridge technical documents.** – dsgdfg Dec 02 '17 at 12:05
  • Additional : `There are also many programs that scan performance, but the results don't match.` – dsgdfg Dec 02 '17 at 12:09
  • INFO: The processor's core power lines control is built in the new generation. In older versions there is a separate phase control/counting circuit. How long does it take to fix the feed phase (instantaneous) when the processor is squatting? A low-frequency counting process (khz) is a complete enigma if you add the processor frequency and multiplier. – dsgdfg Dec 02 '17 at 12:21
  • OK, I've now run this (corrected) code on my laptop (i3 1.7GHz) and it takes over 15 seconds! That's over 100 times slower than my previous report. – quamrana Dec 02 '17 at 16:46
  • Correction: I wasn't comparing the same runs. My Desktop seems to be a consistent 2.5 times faster than my laptop. The reason for my erroneous report was a difference in runtime parameters: `search_path(8 ...` takes much longer than `search_path(6 ...` – quamrana Dec 02 '17 at 17:00

1 Answers1

1

Different computers have different hardwares! Different clock speeds and different RAM sizes and other specifications which may make code run faster!

That is why something called 'Asymptotic Notations' exist in the first place! Since we can't assess the speed or the time it will take to run a code, because every machine will have different specifications, we use 'Asymptotic Notations' as a standard way to explain the time complexity of a give piece of code.

The computer in your office may have different hardware, memory, lower clock-speed and other hardware related contributing factors that causes the running of that exact same code to slow down! Where as a better computer with faster configurations will run the same code much faster.

You're performing a task which is computationally expensive, and requires a lot of memory and processing speed.

ababuji
  • 1,683
  • 2
  • 14
  • 39
  • @Reblochon Masque. Sorry if I'm wrong.. But he emphasized a lot about what he wanted when he asked "Why the execution speed of the same code has a huge difference on the five computers? The result is 0.005, 0.010, 6, 8 and 10 seconds. We can see the difference can be over 1000 times." I assumed then that the code was running correctly if he did get the results at different times, and also mentioned 'the computer in my office'. So its probably the hardware. – ababuji Dec 02 '17 at 09:34
  • He is trying to implement the knight's tour problem.. This is an NP-complete problem. Trying to implement/solve them are computationally expensive.. – ababuji Dec 02 '17 at 09:43
  • Graduate level courses on Algorithms and Data Structures. You? – ababuji Dec 02 '17 at 09:48
  • Have you read OPs question and tried to understand what he's asking? You may be better at writing code than I can ever hope to be. But has he asked you to optimize his code or solve a problem or ask which part of the code are slowing it down? He seems fairly new to StackOverFlow and was probably prompted to post some code when he was framing his question. – ababuji Dec 02 '17 at 09:52
  • I am sorry that I have made a mistake when edited the code on stack overflow. What I can't understand is that why the difference can be about 1000 times and the hardware of computers whose speeds are 6s and 8s are even better than 0.01's. – yuanyuanzijin Dec 02 '17 at 11:38