Today's Question: 


Different programming are similar in helping people build staff work as people want them to. But they all have their own features which differentiate them from other programming languages. The language type may be different, for example Java is OOP, some may have different syntax. A programming motto usually can best describe the characteristic of the programming language. For example, "Write once Run everywhere" for Java.Do you know any other programming language motto?
Do you know the mottos of programmig languages?
Description My Answer

Technical Article => Programming =>  C

Faster than C

Source : Andreas Zwinkau    Date : 2012-03-25 09:12:23  

Judging the performance of programming languages, usually C is called the leader, though Fortran is often faster. New programming languages commonly use C as their reference and they are really proud to be only so much slower than C. Few language designer try to beat C.

What does it take for a language to be faster than C?

Better Aliasing Information

Aliasing describes the fact that two references might point to the same memory location. For example, consider the canonical memory copy:

void* memcopy(void* dst, const void* src, size_t count) {
   while (count--) *dst++ = *src++;
   return dst;

Depending on the target architecture, a compiler might perform a lot of optimizations with this code. For example, on a modern x86 with the SSE instruction MOVDQU, it could copy 16 Byte blocks instead of 4 Byte (sizeof(void*)). Unfortunately, no. Due to aliasing, dst could for example be src+1. In this case, the result must be the first word *src repeated count times at dst. The compiler is not allowed to use MOVDQU due to the semantics of C.

In C99 the restrict keyword was added, which we could use here to encode that src and dst are different from all other references. This mechanism helps in some cases, but not in our example.

Fortran semantics say that function arguments never alias and there is an array type, where in C arrays are pointers. This is why Fortran is often faster than C. This is why numerical libraries are still written in Fortran. However, it comes at the cost of pointer arithmetic.

A language, which wants to be faster than C, should provide semantics, where aliasing can be better analyzed by the compiler.

Push Computation to Compile-Time

Doing things at compile time reduces the run time. Of course, C compilers do this for trivial cases like 1+2, where the addition is already handled at compile time.

However, languages with nice meta-programming support enable the programmer to do similar application specific optimizations. A simple example, we could optimize fib(20) to 6765, without the compiler knowing about Fibonacci numbers.

For a real example, the Eigen C++ library for linear algebra uses C++ templates to avoid copies and be lazy about computations. Of course, Lisp is the grandfather of this technique with its macro system. For example, there is a nice anecdote about a student using Scheme for an assignment. Basically, the programmer can modify the abstract syntax tree during compilation. The trade-off with such meta programming features is complexity. Programmer underestimate the difficulty to write correct macros, like they underestimate the difficulty to write correct concurrent programs.

A language designer should think about meta programming. Something Turing-complete like C++ templates, seems to be beneficial for performance.

Runtime Optimization

At runtime there is dynamic information, which is not available to a static compiler. Any specific example could be duplicated by a C program, but usually it is not feasible. The trick of profile-guided optimizations solves only a small part of the problem.

What becomes especially easy at runtime is whole-world optimization. While this is possible statically, the C semantics (compilation units) and the mandatory preprocessor make it difficult for the compiler. Even Python can beat C by inlining across file borders.

Of course, there are downsides to using a JIT and especially in systems- and embedded programming it is not appropriate. So there might be examples, where Java, C# or others beat C, but they do not threaten C's niche.


Aliasing information is the only one, where I am certain about speed improvements, because it is impossible to reach Fortran-speed in C. The other ideas are more about making it easier to write faster programs.


Save as PDF Mark as read Mark as important
By clicking the "Mark as read" button, this article will be marked as read. It will be removed from the homepage's latest news and the article list on the "Technical article" page in following visits and it will be put to your read list which you can find in "Amin->Article read list". There you can unmark the read articles.
By clicking the "Mark as important" button, this article will be put to your important article list which you can find in "Amin->Article important list". Later when you want reread this article, it's easier for you to find it by checking the "Article important list".

Tags : C,Performance,Speed,Fortran,Criteria   Read(1069) Comment(1) Version(0)

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious 

 Previous : Function Pointers in C are Underrated
 Next : Get associated file icon with only extension name using C++ on Windows

  ::Related Articles

  ::Comment Zone  (Total 1 comment)

Amarieul [Reply]@ 2012-05-02 22:48:24
We use some seriously old soratwfe that needs real-mode DOS to run, so Ie2€™m playing around with DOS emulation on Linux.You wouldn't happen to have a copy of GraphPlus, would you? My dad needs it for an old Textronics instrument from the early 90 s.Welcome to Linux, Gaius, I moved over full time in 2000. My needs are pretty basic, though: web browsing and soratwfe development. For the latter, the free editors, and compilers are a Godsend. Linux is also multiuser from the get go, at work I can ssh into someone else's machine, or to my home machine, and run stuff on it, no problemo, and no worrying about licenses and users. Microsoft is really missing the boat on interoperability and networking.I run fedora myself, but I would recommend Ubuntu to anyone who wants to have multimedia without jumping through hoops. There is a program (I forget its name) that will set it up for you if the newer versions of Ubuntu don't already do so.



:: Other versions

No other versions available yet.

:: Recent articles

:: Most read

:: Contribute

Want to share with the world your understanding about technology? Want to record the process you solve a technical problem? Want to make the world benefit from your understanding and solution? Write them down. You make the world better, the world makes us better.

Write article

:: Find us

Back to top