Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Optimizing array accesses

Reply
Thread Tools

Optimizing array accesses

 
 
Clemens Lode
Guest
Posts: n/a
 
      10-12-2003
Hello!

Is it worth to use a[x] instead of b[y][x] (with a=&(b[y][0]) ?
a is const int*, b is const int.
y is a static int and does only change once (at the beginning of the
program).

That would be one less memory access and thus faster, right?

Thanks
Clemens

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      10-12-2003
"Clemens Lode" <(E-Mail Removed)> wrote in message
news:bmbj1r$8gl$(E-Mail Removed)-karlsruhe.de...
> Is it worth to use a[x] instead of b[y][x] (with a=&(b[y][0]) ?
> a is const int*, b is const int.

b is const int** or const int (*)[someInt] , I assume.

> y is a static int and does only change once (at the beginning of the
> program).
>
> That would be one less memory access and thus faster, right?


It's hard for us to tell if it is "worth" it.
But yes, it will usually be faster to pre-calculate the
address of the sub-array that you will work with.
So it is a good idea, especially if variable 'a' is
given a meaningful name...

Regards,
Ivan
--
http://ivan.vecerina.com




 
Reply With Quote
 
 
 
 
Jon Bell
Guest
Posts: n/a
 
      10-12-2003
In article <bmbj1r$8gl$(E-Mail Removed)-karlsruhe.de>,
Clemens Lode <(E-Mail Removed)> wrote:
>
>Is it worth to use a[x] instead of b[y][x] (with a=&(b[y][0]) ?


Maybe. Maybe not. It depends on the quality of the optimizer in your
particular compiler, and on your particular application.

Check the code generated by your compiler to see what it does under
various levels of optimization. It may already do this particular trick
for you. If not, test both versions of the code and see it it actually
makes a significant difference in speed.

--
Jon Bell <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Desktop accesses laptop and reads folders but Laptop only accesses/opens Desktop but cannot read folders, access is denied onclejon Wireless Networking 3 11-01-2006 10:50 PM
Multi accesses concurrently to a XML file xuanqn09@gmail.com Java 1 04-19-2006 11:34 AM
ASPX accesses to network file resources A.M-SG ASP .Net 4 11-02-2005 01:05 AM
Something accesses the hard disk every 2 sec (longish Leachim Sredna Computer Support 6 05-28-2004 02:24 AM
linux perl accesses ms-access db Gary Perl 0 11-21-2003 06:35 PM



Advertisments