Locality of program

This is a discussion on Locality of program within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I don't understand what locality of program means. I searched in the oxford dicitonary and got this result: AskOxford: locality ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    75

    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?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    In what context did you encounter the phrase "locality of program"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    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
    Location
    Plano, Texas, United States
    Posts
    2,738
    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:

    Code:
    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:

    Code:
    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, 12: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, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21