Thread: Locality of program

  1. #1
    Registered User
    Join Date
    Oct 2008

    Locality of program

    I don't understand what locality of program means.

    I searched in the oxford dicitonary and got this result:
    AskOxford: locality

    noun (pl. localities) 1 an area or neighbourhood. 2 the position or site of something.

    Can someone explain this to me?


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    In what context did you encounter the phrase "locality of program"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    I got it:

    Most programs display a high degree of locality: the same set of pages,the current working set, is being referenced repeatedly.

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Plano, Texas, United States
    Normally in the context of programming, locality refers to variables in use being close to each other in memory.

    Locality is important especially for caching. When I run a program, and I decide to use variable x, the processor does not just fetch variable x from memory, but also everything that is around variable x in memory, and it puts it in the processor cache. It does this because it assumes that much of what we do with variable x will involve variables found very close to x in memory, and in fact if we do something like this:

    void myfunc ( void )
            int x = 0;
            int y = 5;
            int z = 10;
    Chances are they will will be located near each other in memory. One of the classic examples of locality and caching is looping through a matrix. Of course, when you iterate through a matrix the ideal way to do it (under normal circumstances) is in row-major order because elements in the same row of the matrix should be near each other in memory, while elements in different rows are probably much farther apart:

    void myfunc ( void )
            int mat[5][5];
            for ( int y = 0; y < 5; y++ )
                    for ( int x = 0; x < 5; x++ )
                            mat[y][x] = something;
    This causes more cache hits/less cache misses.

    This is how I usually understand locality in a programming context...I don't know if it has any other meaning in the programming world.
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM