Thread: optimization question

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    5

    optimization question

    Hi all,
    what are possible optimizations for this code to make as fast as possible:

    Code:
    unsigned char from[A], to[A];
    
    void foo(){
    
    int i;
    
    for(i=0;i<A;i++)
        to[i]=from[i];
    }
    A is very big.

    thanx

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Where are you getting the values for from[]? How are you going to use the values in to[]? I don't imagine that this copying is your problem, if there's even a problem. Have you profiled your program to see if there's a performance issue and identify the bottlenecks?
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you can try
    if array from[] contains a null-terminated string, use strcpy(). Otherwise you can use memcpy() -- but beware that memcpy() does not null-terminated the string like strcpy() does. In the case of binary data you don't want it null-terminated anyway.
    Code:
    memcpy(to, from,  A);

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Here are a couple more thoughts to consider:

    Rob Pike...offers a slightly different angle in Notes on C Programming:

    Rule 1. You can't tell where a program is going to spend its time. Bottlenecks occur in
    surprising places, so don't try to second guess and put in a speed hack until you've
    proven that's where the bottleneck is.

    Rule 2. Measure. Don't tune for speed until you've measured, and even then don't
    unless one part of the code overwhelms the rest.

    Rule 3. Fancy algorithms are slow when n is small, and n is usually small. Fancy
    algorithms have big constants. Until you know that n is frequently going to be big,
    don't get fancy. (Even if n does get big, use Rule 2 first.)

    Rule 4. Fancy algorithms are buggier than simple ones, and they're much harder to
    implement. Use simple algorithms as well as simple data structures.

    Rule 5. Data dominates. If you've chosen the right data structures and organized things
    well, the algorithms will almost always be self-evident. Data structures, not algorithms,
    are central to programming.[9]

    Rule 6. There is no Rule 6.


    Extracted from The Art of Unix Programming by Eric S. Raymond

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turn Off Optimization?
    By danlee58 in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 03:52 AM
  2. need reading material for c++ database optimization
    By elninio in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2008, 11:32 PM
  3. Optimization settings
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-23-2005, 02:53 AM
  4. Optimization Question
    By saxman in forum C Programming
    Replies: 7
    Last Post: 06-30-2004, 10:49 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM