Jump to content
  • Advertisement
Sign in to follow this  
TimChan

Programming Challenge 1

This topic is 4052 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

Optimize the following code and give detailed reason.
// Initialize iloveseven to an array of 7s
int i, j;
int iloveseven[1024][1024];

for(i = 0; i < 1024; i++) for(j = 0; j < 1024; j++) iloveseven[j] = 7;

Hints: obviously the code has no logical problem. Try to think in a remote area of computer programming. EDIT: more hints: paging, int has size of 4 bytes EDIT2: Check answer on page 2 of this thread [Edited by - TimChan on August 15, 2007 8:43:17 PM]

Share this post


Link to post
Share on other sites
Advertisement
You could pre-increment for a start :P. Though the compiler most likely sorts that out.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
You could pre-increment for a start :P. Though the compiler most likely sorts that out.

Your answer is correct. But there is another answer.

Share this post


Link to post
Share on other sites
Optimize for what — space? Performance?

First thought (probably not what you're looking for, though; technically, I guess it also doesn't function in exactly the same way in that it isn't possible to overflow):


template <typename T> class FilledContainer
{
T fill;

public:
FilledContainer(T Fill) : fill(Fill)
{}

T operator[](unsigned long)
{
return fill;
}
};

// ...

FilledContainer<int> foo(7);

Share this post


Link to post
Share on other sites
iloveseven[j] has better cache coherency, since i changes less frequently than j.

TheUnbeliever: That's cute, I like that.

Share this post


Link to post
Share on other sites
Can you go from i = 0 to i = 1048576 and hop through *iloveseven setting each to 7?

or just iloveseven[1024][1024] = {7}; //can't remember if that works how I'm thinking it does

Share this post


Link to post
Share on other sites
You are misusing the cache.
Try this:
for(j = 0; j < 1024; j++) for(i = 0; i < 1024; i++) iloveseven[j] = 7;

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
iloveseven[j] has better cache coherency, since i changes less frequently than j.

TheUnbeliever: That's cute, I like that.

So what exactly cache coherency means? Your answer is correct and expected but I think the explanation is incomplete.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!