Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reusing an Incremented Variable within a Single Statement

Reusing an Incremented Variable within a Single Statement

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestionlounge
26 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Skippums

    Well actually, I am trying to write something like the following where I am copying from a 0-indexed array to a 1-indexed array:

    const int src[] = {0, 1};
    const size_t srcLen = sizeof(src) / sizeof(int);
    int *const dst = new int[srcLen] - 1;
    for (size_t i = 0; i < srcLen; ++i)
    dst[i + 1] = src[i];

    NOTE: I have simplified the array copy in the above example... I am actually using an external API function to get the value of the src array elements, which is why I am not just using the statement "const int* const dst = &src[0] - 1;". The compiler will probably optimize this by keeping the value of i + 1 from the array assignment for the next loop iteration in a register (as opposed to recomputing it), but if I could write the for-loop as:

    for (size_t i = 0; i < srcLen; )
    dst[++i] = src[i];

    This would *almost* guarantee that i is incremented only once. Given the initial response, I can't have a single statement with two different values for i, so I'm not able to do what I wanted anyway. It would probably have a higher probability of being optimized if I wrote the loop as:

    for (size_t i = 0; i < srcLen; ) {
    const int temp = src[i];
    dst[++i] = temp;
    }

    Almost every compiler available would put temp in a register instead of allocating it on the stack, but I really don't want to write the loop like that. Guess I'll keep my fingers crossed that the compiler optimizes it! (I don't really care timing-wise as the loop is only iterating over a couple hundred thousand elements one time per run, which is an insignificant amount of time as a fraction of the program's run-time, but why not write efficient code when you can?)

    Sounds like somebody's got a case of the Mondays -Jeff

    S Offline
    S Offline
    Stefan_Lang
    wrote on last edited by
    #21

    If your intent is accessing two arrays within a loop, then - no matter the relation between indices - the fastest way would be to use two pointers to the individual elements and increment these. If you're so intent on improving performance, consider this: every direct access to an array element via an index value requires 1. loading the start address of the array 2. get the size of an element 3. multiplying that by the index(-1) 4. adding that to the start address 5. dereference this address to get to the actual element. As opposed to using pointers which just requires 1. load the pointer 2. dereference it Of course, incrementing the pointers eats up most of this advantage, as you have to add the element size each iteration. And most likely a good optimizer will convert your code into something like this anyway. What I want to say, using an index is just complicating things unneccesarily if all you want to do is access each element sequentially.

    1 Reply Last reply
    0
    • S Skippums

      Well actually, I am trying to write something like the following where I am copying from a 0-indexed array to a 1-indexed array:

      const int src[] = {0, 1};
      const size_t srcLen = sizeof(src) / sizeof(int);
      int *const dst = new int[srcLen] - 1;
      for (size_t i = 0; i < srcLen; ++i)
      dst[i + 1] = src[i];

      NOTE: I have simplified the array copy in the above example... I am actually using an external API function to get the value of the src array elements, which is why I am not just using the statement "const int* const dst = &src[0] - 1;". The compiler will probably optimize this by keeping the value of i + 1 from the array assignment for the next loop iteration in a register (as opposed to recomputing it), but if I could write the for-loop as:

      for (size_t i = 0; i < srcLen; )
      dst[++i] = src[i];

      This would *almost* guarantee that i is incremented only once. Given the initial response, I can't have a single statement with two different values for i, so I'm not able to do what I wanted anyway. It would probably have a higher probability of being optimized if I wrote the loop as:

      for (size_t i = 0; i < srcLen; ) {
      const int temp = src[i];
      dst[++i] = temp;
      }

      Almost every compiler available would put temp in a register instead of allocating it on the stack, but I really don't want to write the loop like that. Guess I'll keep my fingers crossed that the compiler optimizes it! (I don't really care timing-wise as the loop is only iterating over a couple hundred thousand elements one time per run, which is an insignificant amount of time as a fraction of the program's run-time, but why not write efficient code when you can?)

      Sounds like somebody's got a case of the Mondays -Jeff

      J Offline
      J Offline
      JFDR_02
      wrote on last edited by
      #22

      "why not write efficient code when you can?" X| Don't be daft. Write readable code when you can. Optimize when necessary and use a profiler to detect inefficencies.

      S 1 Reply Last reply
      0
      • J JFDR_02

        "why not write efficient code when you can?" X| Don't be daft. Write readable code when you can. Optimize when necessary and use a profiler to detect inefficencies.

        S Offline
        S Offline
        Skippums
        wrote on last edited by
        #23

        Why use a performance profiler when I can just guess which parts will be inefficient during implementation? And for the record, my code is readable; the compiler understands it just fine. :-D

        Sounds like somebody's got a case of the Mondays -Jeff

        N 1 Reply Last reply
        0
        • S Skippums

          Why use a performance profiler when I can just guess which parts will be inefficient during implementation? And for the record, my code is readable; the compiler understands it just fine. :-D

          Sounds like somebody's got a case of the Mondays -Jeff

          N Offline
          N Offline
          Niklas L
          wrote on last edited by
          #24

          Skippums wrote:

          Why use a performance profiler when I can just guess which parts will be inefficient

          Unless you're writing Hello World, you will be surprised.

          home

          S 1 Reply Last reply
          0
          • N Niklas L

            Skippums wrote:

            Why use a performance profiler when I can just guess which parts will be inefficient

            Unless you're writing Hello World, you will be surprised.

            home

            S Offline
            S Offline
            Skippums
            wrote on last edited by
            #25

            I guess I need to be more explicit when I type sarcastic comments. Sadly, I couldn't find the appropriate voice inflection characters on my keyboard. :)

            Sounds like somebody's got a case of the Mondays -Jeff

            N 1 Reply Last reply
            0
            • S Skippums

              I guess I need to be more explicit when I type sarcastic comments. Sadly, I couldn't find the appropriate voice inflection characters on my keyboard. :)

              Sounds like somebody's got a case of the Mondays -Jeff

              N Offline
              N Offline
              Niklas L
              wrote on last edited by
              #26

              ..or don't write what I will read :confused: :-D

              home

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups