Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

How useful are compile time functions? Personally I haven't worked in any language that supports them but they seem nifty in some cases.

For those who don't what I mean, a compile-time function is evaluated at compilation rather than at runtime,

so if you have for instance in your code

int i =   Pow(4,34);

you could instead do something like this

int i =   Pow<compilation>(4,34);

and the actual runtime code would be

 int i =   295147905179352825856;

Naturally there are some restrictions on what functions could be run at compile time, they pretty much have to be pure/static but I can also see some instances where it could be used to fetch data into something from a file or database (even though in most cases those things shouldn't be compiled into the code)

What do you think, is a feature like that actually useful or just "cool"? :)

share|improve this question
1  
Isn't this what Lisp macros do? Lisp hackers certainly find them useful, and I've definitely had places where I wanted to do constructs like these in Java code (where the entire result couldn't be precalculated). –  jprete Apr 11 '11 at 15:19
2  
You can do some amazing things with C++ templates. What sort of amazing things you actually should do is more open to question. –  David Thornley Apr 11 '11 at 17:11
add comment

8 Answers

Let's say you're working on an application that have a loop that takes a lot of time because there is some calculations repeated.

Having compile-time evaluation of what you can in this loop can drastically reduce it's execution time. Because the "static" values are computed at compile-time, it is not computed at runtime, and only the values you can't guess at compile-time are really computed at runtime.

Anything that can be done before compile-time avoid you to have to do it at runtime, so it saves you time and memory at runtime. It's basically a powerful implicit optimization feature.

The other benefit is that, like in your example, you can still give name (functions or types) to values that would be hard to understand if were directly written where they were used. That's following the DRY principle and ease the reading.

By the way, in C++ we profit from compile-time code generation by templates. When we create a vector of object and use it's functions, the generated code will be :

  1. the minimal code necessary to do what you need to do with the vector;
  2. optimized at compile-time because the compiler will know a lot about the generated type, in witch context it's used, etc. It will help on performance and still use type-checking to make sure the generated code is "logic" - avoiding a lot of run-time errors that are not simple to debug.
share|improve this answer
add comment

I would call it a optimization technique, and you are right that it can be applied to pure functions only. Generally, a function application

f a1 ... an

can be replaced by its result at compile time, if f is pure and the ai are constant expressions. Note that the function application is itself a constant expression.

share|improve this answer
add comment

I would expect most compilers to make this optimization anyway, certainly in release mode.

Letting the compiler do it is a useful feature for improving performance, I am not sure requiring the developer to flag the method in this way is that useful, compilers are better at this stuff than humans :-).

EDIT Reading the comments below has made me ponder this a little more and realize a couple of things... I am not a compiler expert and perhaps my assumption was a little optimisitc. I can see how it would indeed be hard for a compiler to automaticallly optimise out any non trivial function.

share|improve this answer
    
+1 A good compiler will do it automatically –  MarkJ Apr 11 '11 at 10:20
    
Now that you mention it, why wouldn't the compiler simply optimize stuff like that? Hard to come up with a scenario where you need to explicitly declare a compile-time function –  konrad Apr 11 '11 at 11:05
2  
Strongly depends on the definition of Pow. –  user1249 Apr 11 '11 at 15:15
    
I seem to remember a case where an optimizer was really cautious about floating-point calculations because a different floating-point model could be selected at runtime. –  David Thornley Apr 11 '11 at 17:12
2  
@Steve: I seriously doubt it. This is called SuperCompilation in Haskell (hackage.haskell.org/trac/ghc/wiki/Supercompilation) and it's definitely not easy. Languages that provide mechanisms (like C++ templates) are okay, but automatically detected whether this can be done or not is not trivial. Also, it only works with pure functions called with known arguments (obviously) and most languages (imperative) do not offer the possibility to mark whether a function is pure or not: with the definition out of the way, it's impossible for the compiler to apply this. –  Matthieu M. Apr 11 '11 at 19:29
show 4 more comments

Modern compilers can evaluate static stuff like that at compile-time. For instance, x = 1 + 1 gets optimized by the compiler so the produced cpu instructions would be just x = 2.

A #define macro in C/C++ using only static data seems like a compile time function to me. Or using the inline keyword on a function often results in that as well.

And, yes, they are useful for performance reasons.

share|improve this answer
add comment

Cobol has several compile time options. For instance, you can have the compiler set all uninitialized variables to low-values, or you can have the compiler do nothing, and uninitialized variables would contain whatever was left over in memory from the previous execution. Some Cobol shops liked the low-value initialization, while some didn't. As you might imagine, it sometimes was a culture shock when a Cobol programmer moved from one shop to another.

Here's a list of Cobol compiler options for IBM mainframes.

share|improve this answer
    
Now convince the .NET-folks to upgrade :) –  user1249 Apr 11 '11 at 15:16
    
@Thorbjørn Ravn Andersen: Well, I'd like to see them use .NET to write a process that takes the update transactions from the day and creates a new master transaction tape. :-) Oh, and it has to run at 8 pm Eastern time every weekday night. –  Gilbert Le Blanc Apr 11 '11 at 15:45
add comment

There's an interesting trade-off involved here which I will raise and then leave open for your edification.

Compile-time functions make every build take longer, and execution of the product becomes quicker. Run-time evaluation is great when your programmers' time is expensive, and your users' time is cheap. Compile-time works best when it's the other way around.

share|improve this answer
3  
Alternately, it could be disabled for debug versions and enabled for release versions. –  David Thornley Apr 11 '11 at 17:13
1  
@David agreed, and that's how optimising compilers usually work. The approach proposed by @mko probably couldn't work like that. –  user4051 Apr 11 '11 at 19:02
add comment

When you combine compile time functions with string mixins in the D programming language, they're pretty useful. They allow you to generate arbitrary code at compile time in a way that's transparent to the API user and compile it just like normal code. This is of tremendous value for generative programming.

share|improve this answer
add comment

PL/I allows you to write macros in PL/I itself. Macros are simply PL/I procedures which are called during compile time and whose output is inserted into the source at the point of the call.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.