bitwise operations in obj C

  • hello all,
    I have a modulus operation in my code and it runs fine, but my original
    algorithm avoided division all together for speed reasons - I am using
    modulus due to a change in requirements.

    How exactly (or can I) use bit operations to achieve the same effect and
    increase my speed here ?

    Code:

    line_Number++;

    if(line_Number % increment == 0)
                                              // lineNumber is any integer
    value > 0, can get large w/ time - 100,000++
    {                                          // increment is user selectable
    integer from a set of given values
    ....
    ....
    }

    thanks.
  • Make increment a power of two, and then you can use bit masking (via
    the & operator).
    FWIW I would not worry about one division unless Shark tells you that
    it is a problem. It's not free, but it's not that expensive either.

    On Oct 17, 2007, at 9:34 AM, Erfan Aleemullah wrote:

    > hello all,
    > I have a modulus operation in my code and it runs fine, but my
    > original
    > algorithm avoided division all together for speed reasons - I am using
    > modulus due to a change in requirements.
    >
    > How exactly (or can I) use bit operations to achieve the same
    > effect and
    > increase my speed here ?
    >
    > Code:
    >
    > line_Number++;
    >
    > if(line_Number % increment == 0)
    > // lineNumber is any
    > integer
    > value > 0, can get large w/ time - 100,000++
    > {                                          // increment is user
    > selectable
    > integer from a set of given values
    > ....
    > ....
    > }
    >
    > thanks.
  • On 10/17/07, Erfan Aleemullah <erfan.aleem...> wrote:
    > hello all,
    > I have a modulus operation in my code and it runs fine, but my original
    > algorithm avoided division all together for speed reasons - I am using
    > modulus due to a change in requirements.
    >
    > How exactly (or can I) use bit operations to achieve the same effect and
    > increase my speed here ?

    Are you sure that integer division is actually your bottleneck here?
    Have you profiled you code?

    > Code:
    >
    > line_Number++;
    >
    > if(line_Number % increment == 0)
    > // lineNumber is any integer
    > value > 0, can get large w/ time - 100,000++
    > {                                          // increment is user selectable
    > integer from a set of given values
    > ....
    > ....
    > }

    --
    Clark S. Cox III
    <clarkcox3...>
  • well, not exactly sure, but the program is nearing beta release, and i'm
    trying to eliminate leaks and increase efficiency.  So, not exactly sure if
    i *absolutely need to do bit ops here, but it would definitely help, because
    the operation is done everytime the linenumbers increment themselves - so
    when the line number is 100,000, the code would have 'modulo'ed' 100,000
    times and as the numbers get large themselves--> eg) 100,000 % 12 = slightly
    expensive yes?

    On 10/17/07, Clark Cox <clarkcox3...> wrote:
    >
    > On 10/17/07, Erfan Aleemullah <erfan.aleem...> wrote:
    >> hello all,
    >> I have a modulus operation in my code and it runs fine, but my original
    >> algorithm avoided division all together for speed reasons - I am using
    >> modulus due to a change in requirements.
    >>
    >> How exactly (or can I) use bit operations to achieve the same effect and
    >> increase my speed here ?
    >
    > Are you sure that integer division is actually your bottleneck here?
    > Have you profiled you code?
    >
    >> Code:
    >>
    >> line_Number++;
    >>
    >> if(line_Number % increment == 0)
    >> // lineNumber is any integer
    >> value > 0, can get large w/ time - 100,000++
    >> {                                          // increment is user
    > selectable
    >> integer from a set of given values
    >> ....
    >> ....
    >> }
    >
    > --
    > Clark S. Cox III
    > <clarkcox3...>
    >
  • Can you keep track of line numbers differently?
    int lineNumber;
    int pageNumber;
    int fraction;

    ++lineNumber;
    if (++fraction == increment)
    ++pageNumber;

    On 17-Oct-07, at 10:40 AM, Erfan Aleemullah wrote:

    > well, not exactly sure, but the program is nearing beta release, and
    > i'm
    > trying to eliminate leaks and increase efficiency.  So, not exactly
    > sure if
    > i *absolutely need to do bit ops here, but it would definitely help,
    > because
    > the operation is done everytime the linenumbers increment themselves
    > - so
    > when the line number is 100,000, the code would have 'modulo'ed'
    > 100,000
    > times and as the numbers get large themselves--> eg) 100,000 % 12 =
    > slightly
    > expensive yes?
    >
    > On 10/17/07, Clark Cox <clarkcox3...> wrote:
    >>
    >> On 10/17/07, Erfan Aleemullah <erfan.aleem...> wrote:
    >>> hello all,
    >>> I have a modulus operation in my code and it runs fine, but my
    >>> original
    >>> algorithm avoided division all together for speed reasons - I am
    >>> using
    >>> modulus due to a change in requirements.
    >>>
    >>> How exactly (or can I) use bit operations to achieve the same
    >>> effect and
    >>> increase my speed here ?
    >>
    >> Are you sure that integer division is actually your bottleneck here?
    >> Have you profiled you code?
    >>
    >>> Code:
    >>>
    >>> line_Number++;
    >>>
    >>> if(line_Number % increment == 0)
    >>> // lineNumber is any
    >>> integer
    >>> value > 0, can get large w/ time - 100,000++
    >>> {                                          // increment is user
    >> selectable
    >>> integer from a set of given values
    >>> ....
    >>> ....
    >>> }
    >>
    >>
  • Don't guess. Use Shark and find out what actually takes time. I
    suspect you'll be in for some fun surprises.

    On Oct 17, 2007, at 10:40 AM, Erfan Aleemullah wrote:

    > well, not exactly sure, but the program is nearing beta release,
    > and i'm
    > trying to eliminate leaks and increase efficiency.  So, not exactly
    > sure if
    > i *absolutely need to do bit ops here, but it would definitely
    > help, because
    > the operation is done everytime the linenumbers increment
    > themselves - so
    > when the line number is 100,000, the code would have 'modulo'ed'
    > 100,000
    > times and as the numbers get large themselves--> eg) 100,000 % 12 =
    > slightly
    > expensive yes?
    >
    > On 10/17/07, Clark Cox <clarkcox3...> wrote:
    >>
    >> On 10/17/07, Erfan Aleemullah <erfan.aleem...> wrote:
    >>> hello all,
    >>> I have a modulus operation in my code and it runs fine, but my
    >>> original
    >>> algorithm avoided division all together for speed reasons - I am
    >>> using
    >>> modulus due to a change in requirements.
    >>>
    >>> How exactly (or can I) use bit operations to achieve the same
    >>> effect and
    >>> increase my speed here ?
    >>
    >> Are you sure that integer division is actually your bottleneck here?
    >> Have you profiled you code?
    >>
    >>> Code:
    >>>
    >>> line_Number++;
    >>>
    >>> if(line_Number % increment == 0)
    >>> // lineNumber is any
    >>> integer
    >>> value > 0, can get large w/ time - 100,000++
    >>> {                                          // increment is user
    >> selectable
    >>> integer from a set of given values
    >>> ....
    >>> ....
    >>> }
    >>
    >> --
    >> Clark S. Cox III
    >> <clarkcox3...>
    >>

  • On 10/17/07, Erfan Aleemullah <erfan.aleem...> wrote:
    > well, not exactly sure, but the program is nearing beta release, and i'm
    > trying to eliminate leaks and increase efficiency.
    > So, not exactly sure if i *absolutely need to do bit ops here, but it would
    > definitely help, because the operation is done everytime the linenumbers
    > increment themselves - so when the line number is 100,000, the code would
    > have 'modulo'ed' 100,000 times and as the numbers get large themselves-->
    > eg) 100,000 % 12 = slightly expensive yes?

    Maybe, maybe not. Before you put any effort into speeding up that
    operation, profile your code to see if it is actually the slow part of
    your algorithm.

    --
    Clark S. Cox III
    <clarkcox3...>
  • On 17 Oct 07, at 10:40, Erfan Aleemullah wrote:
    > well, not exactly sure, but the program is nearing beta release,
    > and i'm
    > trying to eliminate leaks and increase efficiency.  So, not exactly
    > sure if
    > i *absolutely need to do bit ops here, but it would definitely
    > help, because
    > the operation is done everytime the linenumbers increment
    > themselves - so
    > when the line number is 100,000, the code would have 'modulo'ed'
    > 100,000
    > times and as the numbers get large themselves--> eg) 100,000 % 12 =
    > slightly
    > expensive yes?

    Integer divide/modulo takes a constant amount of time - it doesn't
    depend on the size of the operands. It's a little slow as far as
    basic mathematical operations go (about 4x as long as a multiply on a
    7450 G4, and a pipeline stall), but it's unlikely to be a bottleneck
    unless you're doing basically nothing else in the loop body.

    There is, incidentally, no way to use bitwise operations to get the
    same effect in a general fashion. However, if you aren't using the
    line number elsewhere, you may get better performance by avoiding the
    modulo entirely:

    if(++line_Number == increment) {
      line_Number = 0;
      // do stuff
    }
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today