Constant array declaration

Started by
15 comments, last by phresnel 13 years, 4 months ago
I need 2 float arrays : sine[360] and cose[360]
The values of these arrays must be sine[x]=sin(x{degrees});
But I would like to save a small bit of performance by declaring them at the start(something like this: sine[360]={0.123,0.123,...} but that would take too much time to put in all the values manually. Anyway of doing this with preprocessor or something?

What I have right now is a loop at the start of my code which calculates sin and cos for every value and sets it.
Advertisement
Why do you think you need to optimize that particular part of your program? How long does it take to run?
It takes a very short amount of time but any optimization is always good, and many small optimizations can add up and become significant.
First, it sounds like you're trying to optimize a problem you don't have. Usually, the optimizing comes when you need it and know where the bottlenecks are.

However, if you don't want to enter the values manually (easily understood) generate a text file in a console app with the format you want and do an #include somewhere, or just a copy and paste. Take you maybe an hour.

You can get into recursive #defines but trouble-shooting those isn't worth the time.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by Necrolyte
It takes a very short amount of time but any optimization is always good, and many small optimizations can add up and become significant.


Emphasis mine. I can't even begin to tell you how wrong that statement is. Perhaps the Wikipedia page on program optimization can provide you with some wisdom.

Actually, this is the kind of "optimization" that can actually slow your code down. If you embed the data manually that information needs to be loaded from disk at runtime. Disk access speeds on modern hardware is several orders of magnitude slower than than processor speeds. For that matter, since processor speeds are significantly faster than memory speeds, using a lookup table in the first place may actually be slower than just doing the calculations.
Quote:Original post by alvaro
Quote:Original post by Necrolyte
It takes a very short amount of time but any optimization is always good, and many small optimizations can add up and become significant.


Emphasis mine. I can't even begin to tell you how wrong that statement is. Perhaps the Wikipedia page on program optimization can provide you with some wisdom.


I read that part about readability. For my current problem doing what I want actually increases readability as it removes a loop from my code.



Also I did what buckeye suggested and my issue is solved
Quote:Original post by SiCrane
Actually, this is the kind of "optimization" that can actually slow your code down. If you embed the data manually that information needs to be loaded from disk at runtime. Disk access speeds on modern hardware is several orders of magnitude slower than than processor speeds. For that matter, since processor speeds are significantly faster than memory speeds, using a lookup table in the first place may actually be slower than just doing the calculations.


What is faster? Loading 2880(float=4 bytes,4*720=2880) bytes from the harddrive or:
loading the part of the code which sets the variables,
and 720 times: converting degrees to radians(1 multiplication) and calculating sin/cos



I'd tend to beleive that loading 2880 bytes is faster
Quote:Original post by Necrolyte
What is faster? Loading 2880(float=4 bytes,4*720=2880) bytes from the harddrive or:
loading the part of the code which sets the variables,
and 720 times: converting degrees to radians(1 multiplication) and calculating sin/cos



I'd tend to beleive that loading 2880 bytes is faster


It really doesn't matter. If you had a bug in the program that generated the included file (say, you used 3.14 for pi or you printed only a few significant digits, and that turns out to not be good enough), you'll have a really hard time finding and fixing the problem. You have made your program more complex without gaining anything measurable in return.

A loop is a lot more readable than 360 mysterious values whose origin is not documented anywhere, and its correctness is much more easily verified. You are just making your code worse.

This is the type of optimization that should only be done when your program turns out to be too slow and a profiler tells you that this is a place where some performance can be gained. Even then, you need to make sure you are actually gaining some performance that justifies the loss of clarity.

After some testing I found I was saving 0.001 seconds by loading the variables rather than calculating them at startup! Timed with Code::Blocks.

I guess this isn't worth having an execuateable file 2kb larger

This topic is closed to new replies.

Advertisement