How to improve performance of this code?

前端 未结 7 779
一生所求
一生所求 2020-11-21 06:54

Thanks to some help from people here, I was able to get my code for Tasmanian camels puzzle working. However, it is horribly slow (I think. I\'m not sure because this is my

7条回答
  •  别那么骄傲
    2020-11-21 07:29

    Replacing

    class node:
        def __init__(self, a, g, p):
            self.arrangement = a
            self.g = g
            self.parent = p
    

    with

    node = collections.namedtuple('node', 'arrangement, g, parent')
    

    dropped the times from around 340-600 msecs to 11.4 1.891 msecs on the input [fCamel, fCamel, gap, bCamel, bCamel]. It yielded the same output.

    This obviously won't help with any algorithmic problems but as far as micro-optimizations go, it isn't bad.

    1 I had the wrong input. There was an extra fCamel that was making it run slower.

提交回复
热议问题