Jump to content
  • Advertisement
Sign in to follow this  
Necrolyte

Constant array declaration

This topic is 2921 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Why do you think you need to optimize that particular part of your program? How long does it take to run?

Share this post


Link to post
Share on other sites
It takes a very short amount of time but any optimization is always good, and many small optimizations can add up and become significant.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!