Array Speed

This is a discussion on Array Speed within the C Programming forums, part of the General Programming Boards category; Is there any speed difference in accessing a static and a dynamic array ? e.x. Code: int *array1 = malloc(100*sizeof(int)); ...

  1. #1
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142

    Array Speed

    Is there any speed difference in accessing a static and a dynamic array ?

    e.x.

    Code:
    int *array1 = malloc(100*sizeof(int));
    
    for(i=0;i<100;i++)
        printf("%d\n",array1[i]);

    Code:
    int array2[100];
    
    for(i=0;i<100;i++)
        printf("%d\n",array2[i]);
    Maybe the speed difference is too little and the only case to see it, is when the loop is giant.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    For most systems, the only difference is the overhead due to the call to malloc.

    On some embedded systems, there can actually be different types of memory (slower flash memory for example) in which there can be a real difference in access time. But on PC's, memory is memory.

    gg

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Sure, memory is memory, but aren't arrays more likely to be kept in the CPU cache than dynamic memory? If it's in the CPU cache, then it would be much faster.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Sure, memory is memory, but aren't arrays more likely to be kept in the CPU cache than dynamic memory? If it's in the CPU cache, then it would be much faster.
    The CPU cache (in x86 processors) will cache stack, globals or local variables exactly the same way - first time the value is read, it gets loaded into the cache, and as long as that location in the cache isn't needed for something else, it stays in there.

    The only situation where you'd set the memory to be uncacheable is where you have a lot of writes and few reads, and there's hardware on the other end of the memory that requires the writes to be fully out in memory before the data can be used by the hardware - this is typically how frame-buffers work for graphcis chips, for example - it's very rare that we read from the graphics frame buffer, but writes are very common.

    But general memory used purely for CPU data or code will be cacheable on any modern(ish) processor, and the way that the memory was allocated (stack or heap) should not make any difference to this.

    It is a slightly different story if we have small amounts of memory scattered about all over the place, say in a linked list, or some other structure with further pointers [e.g. a struct that holds a string which in turn is dynamically allocated] - here we can get into a situation where the data referenced by a pointer is not in the cache, and because the memory is (potentially) fragmented all over the place, the memory fetched to read a link in the linked list will not fetch anything particularly useful NEXT to the link we follow - because the data is scattered all over the memory [particularly if it is a linked list which lives for a long time and has some of the data added and removed during that time].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 01:44 AM

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