编程语言 X 中的一条语句生成多少行机器代码?
在阅读一篇关于失去的编程技能的文章时,作者提出了这样的聊天:
我:你需要多少马力?
SE:我不知道。
我:让我们看看,你的主循环中有多少行代码?
SE:10,000。
我:什么语言?
SE:Fortran
我:好的,每行 Fortran 语言大约有 10 行机器代码,所以 每个循环 100,000 条指令;每个循环执行多少次 第二个?
SE:每 1/20 秒一次。
我:好吧,那就是 20 x 100,000 = 2mops(这比我们拥有的任何东西都快) 当时),也许我们最好重新考虑一下。
这让我想知道,现代语言的编号是多少,比如说 Ruby?如何发现?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为不会有确切的答案。说“对于语言 x,编译的二进制文件每个源代码行有 y 行”。但如果你仍然想知道,也许你可以选择一个大的“不”。编译后的代码和相应的源代码,并找出每个源代码行的平均值。
您可以使用任何二进制编辑器打开二进制文件以查看它生成了多少行。例如。奥利德布格
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
在确定一段代码执行需要多长时间方面,这对于 Fortran 来说甚至不再有效!如果您在 Fortran 90 中编写此代码:
表示
y = EXP(x)
的行可能需要任意长的时间来执行,具体取决于数组x
和的大小>y
。对于任何具有向量赋值的语言也是如此。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:
the line that says
y = EXP(x)
can take arbitrarily long to execute, depending on the size of the arraysx
andy
. The same goes for any language with vector assignment.在聊天中,他们试图估计 CPU 性能。
如果您知道 CPU 性能和循环的执行时间,您可以获得每个循环以及每行的 CPU 命令数。
聊天中的计算并不精确。
即使对于 ruby,您也可以进行类似的不精确计算。
请注意,说一行 Fortran 是 10 个 CPU 命令是错误的,但它是某个循环的平均值,这是事实。
估计 ruby 中的循环所花费的时间。
将 CPU 性能(每秒操作次数)乘以循环时间。您将获得每秒的操作数。
将每秒的操作数除以循环中的行数。这就是你的循环的价值。
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.
对于 X="C#",您可能需要查看
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.