Thread: TCC could reach the same level of GCC and Clang (LLVM) when you write good code?

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    138

    TCC could reach the same level of GCC and Clang (LLVM) when you write good code?

    From what I know, TCC doesn't do any optimizations (or does it?), however,
    after running some simple, small algorithms, it seems that TCC has the same
    runtime performance with GCC and Clang. From what I have heard, both GCC
    and Clang do code analyses and change the resulted code with a more optimized
    version. I'm pretty sure that TCC doesn't do that but I would expect that TCC does
    at least things like Register allocation cause the code would be hundreds of times
    slower than the other two. Recursion is another thing that I know TCC doesn't apply
    optimization to.

    To add on that, I have tried to compile complete programs with TCC vs GCC and the
    runtime performance was something like a 30% loss. So nothing like TCC been 3 times
    slower like it would be in some specific smaller code tests.

    So with that been said, my question would be the following, If we were to write good code,
    would TCC be able to constantly get more than 80% of the runtime of other compilers that
    do analyze and change the code?

    If that's the case, then I prefer to use TCC as a backend and build a better ecosystem. I
    understand how in the era of C without the spread of the internet and the communication,
    we have now, we would need a common effort of people that know optimization tricks.
    But now, we can create and share libraries better and easier than ever! We can create more
    advance programming languages with better mata-programming features, we can easily
    share and improve (contribute) code like never before! So it makes sense to have someone
    dedicate to something and optimize it as much as possible. Tbh, isn't that what people do
    anyways? Wasn't the UNIX philosophy to "do one thing and do it well"? So why have the compiler
    to change your bad code rather than learning how to write quality code yourself?

    And, You're gonna want to write inline assembly if you want the best performance anyways,
    so why having to massively sacrifice compile times for 30% improvement in most NOT low-level
    software? I suppose you wouldn't write a physics engine with it but it can be a great idea to
    create software. What are you guys saying? Am I just been to optimistic and doing big claims
    or what that be practically possible? Of course, I haven't run lots of tests and I don't know
    a lot about compiler specific (that cannot happen at code level) optimizations so some of
    you may have already thought about that. But if my theory can be practically applied, think
    about the possibilities! And well, we can have a tool that scans the code and changes for its
    quality and suggests modification (or either applies it itself) to make it run faster. Rather than
    having the compiler do that every time, we can choose to do that every 2-3 version or after
    a specific period of time (like every 3 months). It's the best of both world.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    There are a multitude of things which call themselves "TCC", which one are you referring to?

    Also, "I tried foo code and observed bar" doesn't say much unless you post code.
    It's very easy to craft (either accidentally or by design) strawman cases which prove or disprove any point you want to make.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Salem View Post
    There are a multitude of things which call themselves "TCC", which one are you referring to?
    I was thinking about "Turbo C (2.01) Compiler" here!

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by flp1969 View Post
    I was thinking about "Turbo C (2.01) Compiler" here!
    If "TCC" is "Turbo C" and not "Tiny C", please uninstall this 37 year old compiler! I don't believe it was that good when it was released 37 years ago!

    Even if it is "Tiny C", stick to modern compilers, gcc & clang! The only two compilers I recommend.

    You can install gcc on Windows through mingw-64 or cygwin.
    Last edited by rstanley; 07-18-2023 at 02:12 PM.

  5. #5
    Registered User
    Join Date
    Oct 2021
    Posts
    138
    Quote Originally Posted by Salem View Post
    There are a multitude of things which call themselves "TCC", which one are you referring to?

    Also, "I tried foo code and observed bar" doesn't say much unless you post code.
    It's very easy to craft (either accidentally or by design) strawman cases which prove or disprove any point you want to make.
    Sorry! I thought it was obvious but it's seems that I'm young, inexperienced and naive. TCC refers to the Tiny C Compiler.

    As for the benchmarks, I have tried a small fibonacci series program and the non-recursion version had the same runtime between TCC and GCC (and I suppose with Clang). Other small algorithms have similar results unless recursion is used (which I mentioned in my OP).

    For the "bigger" programs, I have tried to compile TCC with itself versus GCC and Clang (creating "tcc2"). And then, compare the compiler speed of TCC2 across the different
    version. The results are the following:

    TCC2 compiled with GCC:
    Test 1: 1,27s user 0,25s system 316% cpu 0,479 total
    Test 2: 1,31s user 0,23s system 293% cpu 0,526 total
    Test 3: 1,40s user 0,24s system 309% cpu 0,530 total

    TCC2 compile with Clang:

    Test 1: 1,31s user 0,26s system 312% cpu 0,501 total
    Test 2: 1,31s user 0,22s system 313% cpu 0,490 total
    Test 3: 1,45s user 0,22s system 318% cpu 0,524 total
    TCC2 compile with TCC:
    Test 1: 1,64s user 0,19s system 366% cpu 0,500 total
    Test 2: 1,77s user 0,23s system 358% cpu 0,559 total
    Test 3: 1,72s user 0,23s system 353% cpu 0,551 total
    Even if we take the best GCC scenario and the worse TCC scenario is the base (which of course isn't how it works but anyways), the difference is only something like 18%! Now, it may just be that TCC is as hand optimized as it gets and also, it may have a codebase that doesn't benefit so much from the optimizations of GCC and Clang (in that case, I used "-Ofast -finline -finline-functions -march=native" btw). Keep in mind that I have run this "benchmark" NOW! So, it's fresh! I have done something similar with trying to compile other C programs like "QBE" and "Cproc". From what I remember, the results were much worse in these cases. The same when I did the same test with TCC's codebase some time ago. So, I don't know what's going on but it's interesting! And, I have seen benchmarks where floating point numbers were involved and TCC was VEEEEEERY slow there, so either 1 or 2 specific optimizations and "code transformations" that completely change the results or the GCC libraries have inline assembly?

    Another thing that gave me this idea is the following video: Branchless Programming: Why "If" is Sloowww... and what we can do about it! - YouTube

    So, it talks about branches and why they are slow. At some point, it mentions how the compiler is able to "transform" the code and optimize it a little bit because it knows
    some optimizations strategies. HOWEVER! Then, it was able to make an even optimized version (without using assembly). So, I suppose that the code transformation that
    compiles do are nothing that we cannot do ourselves. If anything, we can do better so way pay the penalty of having to wait more for compile times? The best code will
    be hand written anyways, so why bother? And like I said, we can have a separate analysis tool that we can run as we choose to. Seems like the best approach to me.
    Last edited by rempas; 07-19-2023 at 05:54 AM. Reason: Format mistake fix

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    So, you do not think "TCC" code was optimized to compile better with TCC than with GCC?

    Sorry, but, I think that is a stupid idea if you really believe that.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Oct 2021
    Posts
    138
    Quote Originally Posted by stahta01 View Post
    So, you do not think "TCC" code was optimized to compile better with TCC than with GCC?

    Sorry, but, I think that is a stupid idea if you really believe that.

    Tim S.
    Thanks for reading the post (did you read it all tho because that's crucial?) and replaying Tim and with all due respect
    but if that's what you understood from my post, it seems that you need more time to process it and then reply...

    If you want to give it another try and it ends up been of interest to you, I'll be happy to hear your thoughts about my theories
    and ideas. If there is something that you don't understand, I'll be happy to assist! But based on your reply, I cannot help but
    think that you got the opposite of what I'm trying to say and that's why your reply just doesn't make sense to me.

    If you don't wish to participate however, I'll wish you to have a good day!

    Edit:

    After further investigation from my side, it appear that there is a chance that you talk about my example trying to compile the
    TCC codebase using different compilers? If that's the case, then I own you my apologies!

    In that case, it seems that you say that TCC has optimized its code to compile better with TCC itself? Is that what you suggest?
    Let's make clear with this because at this point, I may be the one that is confused and doesn't understand things...
    Last edited by rempas; 07-19-2023 at 10:21 AM. Reason: Add text

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enabling -Weverything for a part of code in Clang
    By ordak in forum C Programming
    Replies: 5
    Last Post: 01-04-2020, 05:45 AM
  2. Replies: 2
    Last Post: 02-19-2016, 09:30 AM
  3. How to write actually good multithreaded code?
    By MutantJohn in forum Tech Board
    Replies: 23
    Last Post: 11-27-2013, 05:42 PM
  4. is my programming flawless? i need to reach that level of skill
    By Mukul Kumar in forum C++ Programming
    Replies: 64
    Last Post: 05-16-2013, 09:55 AM
  5. Reach out and touch someone - Is this REALLY a good idea?!
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 10-31-2002, 02:49 PM

Tags for this Thread