[Haskell-cafe] Optimization demonstration
Vale Cofer-Shabica
vale.cofershabica at gmail.com
Tue Feb 27 18:14:01 UTC 2018
You can prevent this particular optimization-to-constant by declaring sum '
volatile <https://en.wikipedia.org/wiki/Volatile_(computer_programming)>'
as below:
#include <stdio.h>
int main(void) {
volatile long sum = 0;
for (long i = 1; i <= 100000000L; ++i) {
sum += i;
}
printf("%ld\n",sum);
return 0;
}
I don't know if this provides the comparison you're looking for, but it
does force the compiler to emit assembly to do the summation.
--
vale cofer-shabica
401.267.8253
On Tue, Feb 27, 2018 at 12:26 PM, Ryan Reich <ryan.reich at gmail.com> wrote:
> In other words, it's not competition with the language C but with its
> popular compiler. Choose an example that doesn't simplify and you'll get a
> fairer contest.
>
> On Feb 27, 2018 07:52, "Brandon Allbery" <allbery.b at gmail.com> wrote:
>
>> On Tue, Feb 27, 2018 at 10:06 AM, Dušan Kolář <kolar at fit.vut.cz> wrote:
>>
>>> Unfortunately, in this case the C is much more faster (it prints the
>>> result immediately), at least on my machine. Is it due to a fact that C
>>> compiler does a brutal optimization leading to compile-time evaluation,
>>> while ghc is not able to do that?
>>>
>>>
>> ghc is less prone to invoke that kind of optimization, but sometimes can
>> do so. And yes, gcc is decidedly "brutal" with -O2: inspect the generated
>> assembler and you'll find that it just prints a constant.
>>
>> --
>> brandon s allbery kf8nh sine nomine
>> associates
>> allbery.b at gmail.com
>> ballbery at sinenomine.net
>> unix, openafs, kerberos, infrastructure, xmonad
>> http://sinenomine.net
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20180227/88a60cac/attachment.html>
More information about the Haskell-Cafe
mailing list