Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [tutorial] restoring array quickly
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
vlovich




PostPosted: Sun Jun 01, 2003 2:57 pm   Post subject: [tutorial] restoring array quickly

I came across this when I was doing a game. Basically, I have a screen class. This screen class allows me to cut out rectangles and also check if a given rectangle can fit in the remaining screen. The problem was that every time I had to clear the screen, I had to have two nested for loops and go through each element and set the default value. A simpler workaround turns out to be:

code:

var scr : 0..maxx,0..maxy : int
var scrBkp : 0..maxx,0..maxy:int
var clrCnt := 0

procedure clear
if clrCnt = 0 then
  for x : 0..maxx
  for y : 0..maxy
     scr (x,y) := certainValue
  end for
  end for
  scrBkp := scr
  clrCnt += 1
else
  scr := scrBkp
end if
end clear


This doesn't actually speed up the original clear, but all the consecutive clears should be a bit faster since the compiler should copy one array into another which should be faster than setting each element individually. If anyone has some speed tips to speed up the original clear, I'd be glad to hear them (such as accessing the array in memory directly and sequentially setting its value)
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: