473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Optimizing array accesses

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

Jul 19 '05 #1
2 1453
"Clemens Lode" <gh***@clawsoftware.de> wrote in message
news:bm**********@news.rz.uni-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


Jul 19 '05 #2
In article <bm**********@news.rz.uni-karlsruhe.de>,
Clemens Lode <gh***@clawsoftware.de> 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 <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: corey | last post by:
In PHP Version 4.3.6 I am trying get a value from a class member that is an associative array... class MyClass { $var row; function MyClass() { $this->$row = array("A"=>"Apple",...
5
by: ArShAm | last post by:
Hi there Please help me to optimize this code for speed I added /O2 to compiler settings I added /Oe to compiler settings for accepting register type request , but it seems that is not allowed...
3
by: PWalker | last post by:
Hi, I have written code that I would like to optimize. I need to push it to the limit interms of speed as the accuracy of results are proportional to runtime. First off, would anyone know any...
8
by: Hagen | last post by:
Hi, I have a question that you probably shouldn´t worry about since the compiler cares for it, but anyways: When you run your compiler with optimization turned on (eg. g++ with -Ox flag) and...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
36
by: Eric Laberge | last post by:
Hi! I'm working on automatically generated code, and need to assign arrays. memcpy is an obvious solution, but it becomes complicated to use in the context I'm working on, ie.: I could use it...
3
by: Nick Gilbert | last post by:
Hi, I have to send an array of prices for a list of products over XML. Currently my XML data looks like this: <ArrayOfProd> <Prod Code="productcode001"> <Prices> <P F="2005-01-01"...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.