Why Java is running faster than C here?

后端 未结 9 769
甜味超标
甜味超标 2021-01-02 11:55

Inspired by this question,

Now visible only for users with > 10k rep

I came up with the following code:

$cat loop.c 
int main(          


        
9条回答
  •  一向
    一向 (楼主)
    2021-01-02 12:39

    Because the program doesn't do anything, an optimizer can remove the loop


    If you are trying to make a compiler do a certain unit or, well, benchmark of work, then you need to fool it into thinking the result of the work will actually be used.

    One way to do this is to write a function in one file, compile it, and then call it with the setup from another file. No compiler can anticipate what will be compiled in the future.

    Without that, it's just sort of a contest between default optimization levels and has no useful significance.

提交回复
热议问题