Thread: Interview Question

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Interview Question

    Hey Guys,

    I am new to C board and wish I wasn't since it seems like there is a ton of good information presented here. In any case, I just finished an interview today and I was asked a particular c problem that stumped me and when I thought I was right, I was wrong. But still the interviewers explanation of why he was right does not convince me and that's why to further my understanding and stay true to the C programming language I want to get more experienced C programmer's input as to what is really going on.

    So basically the premise of the question:
    Compare the two code snippets below. What is it that they do and why is one faster than the other?
    Code:
    foo(char *_a, const char *_b, int _count)
    {
    do{
    *_a++ = *_b++;
    }while(--_count > 0);
    }
    Code:
    foo2(char *_a, const char *_b, int _count)
    {
    int n = (_count + 7)/8;
    switch(_count % 8) {
    case 0:  do { *_a++ = *_b++;
    case 7: *_a++ = *_b++;
    case 6: *_a++ = *_b++;
    case 5: *_a++ = *_b++;
    case 4: *_a++ = *_b++;
    case 3: *_a++ = *_b++;
    case 2: *_a++ = *_b++;
    case 1: *_a++ = *_b++;
    }while(--n> 0);
    }
    }
    My reasoning:
    Both functions are essentially doing the same thing: the pointer a is pointing to data stored into data pointed by b.

    The first one is faster because it has a lot less statements and will branch a lot less. Where as the second one deals with a lot of comparisons in the switch statements before it does any of its data manipulation. Therefore the second one implements comparisons/branch instructions and loops before it copies data and the first one just has one while loop without any branching thus is more efficient.

    The interviewers said the second one is much better because it reduces the loop by an order of n/8, where n is the number of loops. I don't really see how this works, I can see how the idea is more efficient, but to me the it seems the second code is working harder for no reason at all and I don't get how it achieves that

    If there is something I am missing here please let me know, cause I still don't see it. Thanks.
    Last edited by nndhawan; 02-17-2011 at 01:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c Interview question.
    By bkankur in forum C Programming
    Replies: 3
    Last Post: 12-10-2009, 07:53 AM
  2. SDL buffer channels question
    By TriKri in forum Game Programming
    Replies: 3
    Last Post: 12-09-2009, 05:52 PM
  3. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  4. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM