I have a program for some scientific simulation stuff, and as such needs to run quickly.
When I started out, I was somewhat lazy, and decided to allow inputting constants later; and just used #define macros for them all.
The problem is that when I tried changing that, it got a lot slower. For example, changing
#define WIDTH 8
//..... code
to
#define WIDTH width
int width;
//... main() {
width=atoi(argv[1]);
//...... code
resulted in something that used to take 2 seconds taking 2.8. That’s just for one of about a dozen constants, and I can’t really afford that even. Also, there is probably some complied-away math with these.
So my question is if I can have some way (bash script?) of compiling the constants I want to use into the program at runtime. It’s ok if any machine that needs to run this has to have a compiler on it. It currently compiles with a standard (quite simple) Makefile.
–This also allows for march=native, which should help a little.
I suppose my question also is if there’s a better way of doing it entirely…
At least if I understand your question correctly, what I’d probably do would be something like:
(and likewise for the other constants you want to be able to modify). The in your makefile(s), add some options to the makefile to pass the correct definitions to the compiler when/if necessary, so if you wanted to change the WIDTH, you’d have something like:
and when you compile the file, this would be used as the definition for WIDTH, but if you didn’t define a value in the makefile, the default in the source file would be used.