开发者

How many lines of machine code are generated by one statement in programming language X?

Reading an article about Lost Programming Skills, the author brings up this chat:

Me: How much horsepower do you need?

SE: I don't know.

Me: Let's see, how many lines of code in your main loop?

SE: 10,000.

Me: what language?

SE: Fortran

Me: ok, that's about 10 lines of machine code per line of Fortran, so 100,000 instructions per loop; how many times does the loop execute per second?

SE: every 1/20th of a second.

Me: OK, so that's 20 x 100,000 = 2mops (which was faster than anything we had at开发者_如何学Go the time), maybe we'd better rethink this.

Which makes me wonder, what is the number for modern languages, say Ruby? How does one find out?


i dont think there would be an exact no. saying "for languange x the compiled binary has y lines per source code line". But if you still want to find out may be you can take a large no. of compiled code and corresponding source code and find out the average per source code line. You can open the binary with any binary editor to see how many lines it generates. for eg. Ollydbg


In terms of determining how long a piece of code will take to execute, that doesn't even really work for Fortran any more! If you write this in Fortran 90:

SUBROUTINE foo(x, y)
  IMPLICIT NONE
  REAL, DIMENSION(:), INTENT(IN) :: x
  REAL, DIMENSION(:), INTENT(OUT) :: y
  y = EXP(x)
END SUBROUTINE foo

the line that says y = EXP(x) can take arbitrarily long to execute, depending on the size of the arrays x and y. The same goes for any language with vector assignment.


In the chat they where trying to estimate CPU performance. If you know CPU performance and time of execution of the loop you can get number of CPU commands per loop and then per line. Calculation in your chat is not precises. You can do similar unprecise calculations even for ruby. Be aware that it wrong to say that one fortran line is 10 CPU commands BUT is average for certain loop it was true.

Estimate time taken by your loop in ruby. Multiply your CPU performance (in operations per second) on loop time. You will get operations per second. Divide operations per second on number of lines in loop. That is your value for your loop.


For X="C#" you might want to take a look at Faster Managed Code: Know What Things Cost from Microsoft. It says, that (particular) modern languages are heavily optimized before actually touching the hardware.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜