Performance: Matlab vs Python

前端 未结 5 1928
长情又很酷
长情又很酷 2020-11-29 13:06

I recently switched from Matlab to Python. While converting one of my lengthy codes, I was surprised to find Python being very slow. I

5条回答
  •  感情败类
    2020-11-29 13:32

    Upon further investigation I have found that using indices as indicated in the answer is still slower.

    Solution: Use meshgrid

    def exampleKernelA(M, x, N, y):
        """Example kernel function A"""
        # Euclidean norm function implemented using meshgrid idea.
        # Fastest
        x0, y0 = meshgrid(y[:, 0], x[:, 0])
        x1, y1 = meshgrid(y[:, 1], x[:, 1])
        # Define custom kernel here
        kernel = sqrt((x0 - y0) ** 2 + (x1 - y1) ** 2)
        return kernel
    

    Result: Very very fast, 10 times faster than indices approach. I am getting times which are closer to C.

    However: Using meshgrid with Matlab beats C and Numpy by being 10 times faster than both.

    Still wondering why!

提交回复
热议问题