C in Real Life

This is a discussion on C in Real Life within the C Programming forums, part of the General Programming Boards category; After Gustaff's post about higher level languages possibly having better run time, I remembered a thread we had here, that ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    C in Real Life

    After Gustaff's post about higher level languages possibly having better run time, I remembered a thread we had here, that dealt with just that situation.

    The thread ran to 5 pages, so I'll pull the highlights together (full thread link):
    Python --> C

    "Macha" had a problem. His company had hundreds of thousands of tuples that had to be looked at, and those with two or more matching values with any other tuple, needed to be noted, for more analysis.

    Macha was familiar with Python, but found the run-time to be VERY long. We asked for, and received from him, a sample file of tuples, that his Python program ran in 4.9 seconds.

    Quickly, Salem slaughtered that run time. Perhaps not unexpected, because Salem is a very experienced C programmer. More telling imo, is that two other programmers also did the same thing. My programs run time was 0.05 seconds on his Python data that had a run-time of 4.9 seconds. I'm a hobby C programmer, not a pro.

    Later, we created a much larger file, that ran in 3.2 seconds in C. This was the next bit of dialog:

    Quote Originally Posted by Adak
    How does 3.2 sec's compare with Python's time on the larger file?
    Quote Originally Posted by Macha
    It's not even worth waiting for it. It's well well beyond hard-boiled-egg-time.
    In the end, MK27 and others, also found an algorithm improvement for his Python program. The C program, running in C#, still took 15 seconds, even after this improvement. That's 4.69 times as long as the C run-time.

    You'll see this over and over again, if you hang around mixed language programming forums and newsgroups. People finding nice speed-ups to their program, when it's set up to run in C or C++. It's not a coincidence.

    I have never heard of a program, that ran slower, after being changed over from an interpreted language, to C. It could happen for the reasons Gustaff stated (poor choice of an algorithm in C), but I've never heard of it, except from inexperienced C/C++ programmers.

    C programmers learn more about algorithms, because we do "roll our own". After awhile, we learn to "roll", rather better than you would expect.

    I believe you'd be hard pressed to find time-critical programs, that haven't been switched over to run in a compiled language - especially C or C++, as soon as possible.
    Last edited by Adak; 08-30-2010 at 05:08 AM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,144
    O_o

    Wow. Are you really this insecure about the performance of C as a programming language?

    *shrug*

    I hate to bring it up, but with the advent of virtual machines using registers, "JIT", late stage optimizations, and native tuning all the old arguments and comparisons of interpreted languages and compiled languages are irrelevant.

    ^_^;

    Okies. I admit it. I enjoyed bringing up, and I'm very curious to see what happens next.

    Soma

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Adding on what Adak said I think the very fact that the mainstream Python implementation CPython is written in C speaks miles on the performance of C. Python is a nice language and has some very useful features(particularly in terms of list processing) but in terms of actual performance it will always be inferior to C because of 2 simple reasons:

    1) C is compiled while Python is interpreted(although there is some work done on Python compilers)
    2) C is so low-level that you can almost smell the CPU registers. In fact, you can even choose to store something small on a register rather than on the stack or heap.

    On the other hand, Python might have an edge on C in terms of productivity (easier to debug, more readable, more abstraction, etc.). However, sigh, I feel like many people are confused about the difference between these two magic 'P' words: productivity vs performance.

    In terms of algorithms, I think Adak said what we all know to be true:

    "C programmers learn more about algorithms, because we do "roll our own". After awhile, we learn to "roll", rather better than you would expect. "
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If I might chirp in here...

    I've done some comparisons between C and other compiled languages as well, notably OOP based products such as Delphi. I've also compared it to Pascal (Delphi at winapi level).

    It's been my experience that C (I use the Pelles flavor) will almost always produces smaller and faster code. Programs that would take several megabytes in OOP code come down to a couple of hundred K in C... and usually run circles around their bigger brothers.

    The C vs Pascal comparison was interesting... the two compilers produced virtually identical files but somehow the C version was just a hair faster.

    What really interests me in all this is that C is one of the oldest languages still in use... Living proof that newer is not always better.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Appreciate your input, but Delphi (and Pascal), are compiled, not interpreted, languages:**

    What is Delphi?
    Borland Delphi is a high-level, compiled, strongly typed language that supports structured and object-oriented design.
    Although C is somewhat faster than Delphi in most programs, my "gripe" is with the naive claim that interpreted languages run as fast overall, as C.

    Another data point for this is in chess programming, where blazing speed is so frequently the difference between a successful program, or a commercial or personal, embarrassment.

    I don't believe the rumors about chess programmers selling their relativess into slavery, in exchange for a bit more speed for their programs.

    You get the idea - your program is looking 3 moves ahead, but your opponent's program is looking 4 moves ahead - your chances of winning are decidedly *bleak*.

    Speed is crucial to success. The algorithms used are complex - evaluations are based on a myriad of chess themes, based on an incomplete search and preliminary estimates.

    So how many of the top 200 chess programs run on an interpreted language? How many use these "superior" libraries, JIT, and any of the host of great features in the interpreted languages?

    None

    What languages are used? C, and C++, (with portions done in assembly).

    Of course.

    **CommonTater stated Delphi was a compiled language, but I wanted to emphasize that point. Clumsily worded on my part.
    Last edited by Adak; 08-31-2010 at 12:14 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Appreciate your input, but Delphi (and Pascal), are compiled, not interpreted, languages:
    Yes I know that, and said so in my commentary.

    Although C is somewhat faster than Delphi in most programs, my "gripe" is with the naive claim that interpreted languages run as fast overall, as C.
    How could they... it takes time to churn token code ... lots of it.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Implementations using JIT can theoretically be faster than compiled language because it can optimize the program at run time, using real run-time profile.

    The magic is basically to compile parts of the file into machine code at run time.

    Interpreted languages used to be a few hundred times slower than compiled. With JIT they can come as close as 2x or 3x slower.

    The difference is small enough for many applications to not care.

    If Moore's "law" holds true, 2x slower is just like using a computer from 1.5 years ago. That doesn't sound too bad.

    I have no doubt that in the near future, it would go even closer. Would probably take a while for it to surpass, if ever, but the gap is getting smaller very fast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scientists create artificial life
    By Brafil in forum General Discussions
    Replies: 171
    Last Post: 05-26-2010, 01:09 AM
  2. Game of Life... Turned my life to HELL!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-21-2009, 06:29 PM
  3. %16 with double
    By spank in forum C Programming
    Replies: 11
    Last Post: 03-05-2006, 09:10 PM
  4. Why am I getting 'undelcared identifier' ???
    By Bill83 in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2006, 12:00 PM
  5. How would the online-world be in real life?
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 06-02-2005, 02:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21