comparing python with c/fortran

后端 未结 8 2135
深忆病人
深忆病人 2021-02-04 10:15

I wrote the following programs to compare the speed of python with c/fortran. To get the time used by the programs I used the \"time\" command. All the programs compute the sq

8条回答
  •  青春惊慌失措
    2021-02-04 10:49

    There are a number of things you should be aware of before you start comparing timings like that.

    1. As mentioned in another answer, it could be that the compiler optimizes the loop and the actual value away. Furthermore, even if you print the result, it could just pre-compute the square root.
    2. You are using real in Fortran and float in C, so (depending on your system of course) the compiler will probably use the sqrtf library call in fortran, while in C you use sqrt instead of sqrtf, which you should use for a float.
    3. In Python, you should use the numpy and scipy packages, they provide arrays on which you can do efficient whole-array operations, avoiding the looping in Python.

提交回复
热议问题