Unusual Speed Difference between Python and C++

后端 未结 17 2296
庸人自扰
庸人自扰 2020-12-22 21:25

I recently wrote a short algorithm to calculate happy numbers in python. The program allows you to pick an upper bound and it will determine all the happy numbers below it.

17条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-22 21:57

    I am not an expert at C++ optimization, but I believe the speed difference may be due to the fact that Python lists have preallocated more space at the beginning while your C++ vectors must reallocate and possibly copy every time it grows.

    As for GMan's comment about find, I believe that the Python "in" operator is also a linear search and is the same speed.

    Edit

    Also I just noticed that you rolled your own pow function. There is no need to do that and the stdlib is likely faster.

提交回复
热议问题