// 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;
Programming Challenge 1
Optimize the following code and give detailed reason.
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]
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.
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):
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);
iloveseven[j] has better cache coherency, since i changes less frequently than j.
TheUnbeliever: That's cute, I like that.
TheUnbeliever: That's cute, I like that.
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
or just iloveseven[1024][1024] = {7}; //can't remember if that works how I'm thinking it does
// Initialize iloveseven to an array of 7sshort i, j;char iLoveseven[1024][1024];for(i = -1; i < 1024; ++i) for(j = -1; j < 1024; ++j) iLoveseven[j] = 7;
You are misusing the cache.
Try this:
for(j = 0; j < 1024; j++) for(i = 0; i < 1024; i++) iloveseven[j] = 7;
Try this:
for(j = 0; j < 1024; j++) for(i = 0; i < 1024; i++) iloveseven[j] = 7;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement