Stride of an array
Encyclopedia
In computer programming
, the stride of an array (also increment or step size) refers to the number of locations in memory between successive array elements, measured in byte
s or in units of the size of the array's elements.
An array with stride 1 has elements which are contiguous in memory. Such arrays are sometimes said to have unit stride. Unit stride arrays are generally more efficient than non-unit stride arrays, due to the effects of caching
. This is attributed to the Principle of Locality
, specifically spatial locality.
and C++
) allow structures to be padded
to better take advantage of the word length of the machine. For example:
In the above code snippet,
architecture, and the compiler had optimized (as is usually the case) for minimum processing time rather than minimum memory usage.
Second, some languages allow arrays of structures to be treated as overlapping parallel array
s with non-unit stride:
This idiom is a form of type punning
.
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
, the stride of an array (also increment or step size) refers to the number of locations in memory between successive array elements, measured in byte
Byte
The byte is a unit of digital information in computing and telecommunications that most commonly consists of eight bits. Historically, a byte was the number of bits used to encode a single character of text in a computer and for this reason it is the basic addressable element in many computer...
s or in units of the size of the array's elements.
An array with stride 1 has elements which are contiguous in memory. Such arrays are sometimes said to have unit stride. Unit stride arrays are generally more efficient than non-unit stride arrays, due to the effects of caching
Cache
In computer engineering, a cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere...
. This is attributed to the Principle of Locality
Locality of reference
In computer science, locality of reference, also known as the principle of locality, is the phenomenon of the same value or related storage locations being frequently accessed. There are two basic types of reference locality. Temporal locality refers to the reuse of specific data and/or resources...
, specifically spatial locality.
Reasons for non-unit stride
There are at least two reasons arrays may have a stride larger than their elements' width in bytes. First, many languages (including CC (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
) allow structures to be padded
Data structure alignment
Data structure alignment is the way data is arranged and accessed in computer memory. It consists of two separate but related issues: data alignment and data structure padding. When a modern computer reads from or writes to a memory address, it will do this in word sized chunks...
to better take advantage of the word length of the machine. For example:
In the above code snippet,
myArray
might well turn out to have a stride of four bytes, rather than three, if the C code were compiled for a 32-bit32-bit
The range of integer values that can be stored in 32 bits is 0 through 4,294,967,295. Hence, a processor with 32-bit memory addresses can directly access 4 GB of byte-addressable memory....
architecture, and the compiler had optimized (as is usually the case) for minimum processing time rather than minimum memory usage.
Second, some languages allow arrays of structures to be treated as overlapping parallel array
Parallel array
In computing, a parallel array is a data structure for representing arrays of records. It keeps a separate, homogeneous array for each field of the record, each having the same number of elements. Then, objects located at the same index in each array are implicitly the fields of a single record....
s with non-unit stride:
This idiom is a form of type punning
Type punning
In computer science, type punning is a common term for any programming technique that subverts or circumvents the type system of a programming language in order to achieve an effect that would be difficult or impossible to achieve within the bounds of the formal language.In C and C++, constructs...
.