Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 103609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:09:03+00:00 2026-05-11T01:09:03+00:00

I always wonder why compilers can’t figure out simple things that are obvious to

  • 0

I always wonder why compilers can’t figure out simple things that are obvious to the human eye. They do lots of simple optimizations, but never something even a little bit complex. For example, this code takes about 6 seconds on my computer to print the value zero (using java 1.6):

int x = 0; for (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {     x += x + x + x + x + x; } 
System.out.println(x); 

It is totally obvious that x is never changed so no matter how often you add 0 to itself it stays zero. So the compiler could in theory replace this with System.out.println(0).

Or even better, this takes 23 seconds:

public int slow() {    String s = "x";    for (int i = 0; i < 100000; ++i) {        s += "x";    }    return 10; } 

First the compiler could notice that I am actually creating a string s of 100000 "x" so it could automatically use s StringBuilder instead, or even better directly replace it with the resulting string as it is always the same. Second, It does not recognize that I do not actually use the string at all, so the whole loop could be discarded!

Why, after so much manpower is going into fast compilers, are they still so relatively dumb?

EDIT: Of course these are stupid examples that should never be used anywhere. But whenever I have to rewrite a beautiful and very readable code into something unreadable so that the compiler is happy and produces fast code, I wonder why compilers or some other automated tool can’t do this work for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:09:04+00:00Added an answer on May 11, 2026 at 1:09 am

    Oh, I don’t know. Sometimes compilers are pretty smart. Consider the following C program:

    #include <stdio.h>  /* printf() */  int factorial(int n) {    return n == 0 ? 1 : n * factorial(n - 1); }  int main() {    int n = 10;     printf('factorial(%d) = %d\n', n, factorial(n));     return 0; } 

    On my version of GCC (4.3.2 on Debian testing), when compiled with no optimizations, or -O1, it generates code for factorial() like you’d expect, using a recursive call to compute the value. But on -O2, it does something interesting: It compiles down to a tight loop:

        factorial:    .LFB13:            testl   %edi, %edi            movl    $1, %eax            je  .L3            .p2align 4,,10            .p2align 3    .L4:            imull   %edi, %eax            subl    $1, %edi            jne .L4    .L3:            rep            ret 

    Pretty impressive. The recursive call (not even tail-recursive) has been completely eliminated, so factorial now uses O(1) stack space instead of O(N). And although I have only very superficial knowledge of x86 assembly (actually AMD64 in this case, but I don’t think any of the AMD64 extensions are being used above), I doubt that you could write a better version by hand. But what really blew my mind was the code that it generated on -O3. The implementation of factorial stayed the same. But main() changed:

        main:    .LFB14:            subq    $8, %rsp    .LCFI0:            movl    $3628800, %edx            movl    $10, %esi            movl    $.LC0, %edi            xorl    %eax, %eax            call    printf            xorl    %eax, %eax            addq    $8, %rsp            ret 

    See the movl $3628800, %edx line? gcc is pre-computing factorial(10) at compile-time. It doesn’t even call factorial(). Incredible. My hat is off to the GCC development team.

    Of course, all the usual disclaimers apply, this is just a toy example, premature optimization is the root of all evil, etc, etc, but it illustrates that compilers are often smarter than you think. If you think you can do a better job by hand, you’re almost certainly wrong.

    (Adapted from a posting on my blog.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a question that when programming I always wonder: What to use when
I always believe they did, but seeing some answers here make me doubt... Can
Whenever I design a database, I always wonder if there is a best way
I distribute software online, and always wonder if there is a proper way to
Even though I always strive for complete validation these days, I often wonder if
I always seem to have a hard time starting a new Firefox extension. Can
I wonder how a can include an pre-compiled binary in an iPhone application. I
I always wonder why I must write foreach my $x (@arr) instead of foreach
I wonder why the name after the #ifndef directive is always all caps and
I always wonder, should we use NSClassFromString before using any built in class in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.