• programming C The "C is Efficient" Language Fallacy scienceblogs.com
    The Objective-Caml bytecode interpreter was faster than the carefully hand-optimized C program! Why? Because the OCaml compiler could recognize that the arrays were completely independent — it didn't need to worry about one iteration of the loop stepping on the values used by another. The C compiler couldn't apply a lot of useful optimizations, because it couldn't be sure that they were valid.
    а что разве в современных компиляторах нет никаких костылей, чтобы помогать компилятору понимать такие вещи?
    ♡ recommended by @Minoru

Replies (5)

  • @nobodyzzz, Есть. Называется "чистый язык" :3
  • @L29Ah, в комментариях на hn нашлось As of C99, of course, C has the "restrict" keyword which allows pointers to explicitly be declared to not alias, thus enabling all these optimizations in C, too.
  • @nobodyzzz, pure
    Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. These functions should be declared with the attribute pure. For example,

    int square (int) __attribute__ ((pure));


    says that the hypothetical function square is safe to call fewer times than the program says.

    Some of common examples of pure functions are strlen or memcmp. Interesting non-pure functions are functions with infinite loops or those depending on volatile memory or other system resource, that may change between two consecutive calls (such as feof in a multithreading environment).

    The attribute pure is not implemented in GCC versions earlier than 2.96.
    const
    Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict class than the pure attribute above, since function is not allowed to read global memory.

    Note that a function that has pointer arguments and examines the data pointed to must not be declared const. Likewise, a function that calls a non-const function usually must not be const. It does not make sense for a const function to return void.

    The attribute const is not implemented in GCC versions earlier than 2.5. An alternative way to declare that a function has no side effects, which works in the current version and in some older versions, is as follows:

    typedef int intfn ();

    extern const intfn square;


    This approach does not work in GNU C++ from 2.6.0 on, since the language specifies that the `const' must be attached to the return value.

    На ещё, правда это GCC C.
  • @L29Ah, Офигеть!
  • @Minoru, Почитай список расширений гнуси: там много вкусного.