Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #127652

Re: Copy array with small changes

From Spiros Bousbouras <spibou@gmail.com>
Newsgroups comp.lang.c
Subject Re: Copy array with small changes
References <p848dh$oko$1@dont-email.me> <tRhpC.69712$4z5.40147@fx17.iad>
Message-ID <BaipC.249658$eQ7.120439@fx46.am4> (permalink)
Organization virginmedia.com
Date 2018-03-11 22:35 +0000

Show all headers | View raw


On Sun, 11 Mar 2018 18:12:41 -0400
Richard Damon <Richard@Damon-Family.org> wrote:
> On 3/11/18 5:51 PM, Noob wrote:
> > Hello,
> > 
> > I'm writing a toy program where I have a function that copies
> > a small sorted array (less than 12 ints) while making two small
> > changes: remove two elements, insert one element.
> > 
> > The function prototype is:
> > 
> > void update_array(int *arr, int n, int zi, int zj, int val);
> > 
> > arr = the original array, sorted by increasing values
> > n = number of elements < N = 12
> > zi = 1st index to remove
> > zj = 2nd index to remove
> > val = new value to insert while keeping the array sorted
> > 
> > Here's my current implementation:
> > 
> > void update_array(int *arr, int n, int zi, int zj, int val)
> > {
> >      int new_arr[N];
> >      int i, j = 0, done = 0;
> > 
> >      for (i = 0; i < n; +i)
> >      {
> > 	if (i == zi || i == zj) continue;
> > 	int tmp = arr[i];
> > 	if (!done && val < tmp) {
> > 	    new_arr[j++] = val;
> > 	    done = 1;
> > 	}
> >          new_arr[j++] = tmp;
> >      }
> >      new_arr[j] = val;
> >      do_something(new_arr);
> > }
> > 
> > It's ugly IMO. Do you have suggestions for improvements?
> > This function is called several billion times, so any performance
> > improvement will be useful.
> > 
> > Regards.
> > 
> 
> For some compilers/processors, rather than using indexing, you could 
> just keep a pointer to the 'current' element of the array, and rather 
> than bumping the index, bump the pointer (and for the input array, also 
> decrement the length, and items to drop).

In this day and age are there really any compilers which won't do on their
own the transformation between  pointer  and  base+index  based on what is
faster ? Regardless , I don't see what the terminating test would be that
would require to increment the pointer and decrement the size of the array.

> A second small improvement would be unrolling the done test and using 
> two loops, the first only for items less than val, and the second for 
> the items after you have copied val.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Copy array with small changes Noob <root@127.0.0.1> - 2018-03-11 22:51 +0100
  Re: Copy array with small changes supercat@casperkitty.com - 2018-03-11 15:11 -0700
  Re: Copy array with small changes Richard Damon <Richard@Damon-Family.org> - 2018-03-11 18:12 -0400
    Re: Copy array with small changes Spiros Bousbouras <spibou@gmail.com> - 2018-03-11 22:35 +0000
      Re: Copy array with small changes Richard Damon <Richard@Damon-Family.org> - 2018-03-13 13:10 -0400
  Re: Copy array with small changes Spiros Bousbouras <spibou@gmail.com> - 2018-03-11 22:13 +0000
    Re: Copy array with small changes Spiros Bousbouras <spibou@gmail.com> - 2018-03-12 00:04 +0000
    Re: Copy array with small changes bartc <bc@freeuk.com> - 2018-03-12 10:35 +0000
      Re: Copy array with small changes Spiros Bousbouras <spibou@gmail.com> - 2018-03-12 12:37 +0000
        Re: Copy array with small changes bartc <bc@freeuk.com> - 2018-03-12 13:10 +0000
        Re: Copy array with small changes Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-03-12 13:34 +0000
          Re: Copy array with small changes Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-17 09:22 -0700
        Re: Copy array with small changes mark.bluemel@gmail.com - 2018-03-12 07:47 -0700
          Re: Copy array with small changes bartc <bc@freeuk.com> - 2018-03-12 14:56 +0000
            Re: Copy array with small changes Spiros Bousbouras <spibou@gmail.com> - 2018-03-12 15:14 +0000
            Re: Copy array with small changes mark.bluemel@gmail.com - 2018-03-12 08:53 -0700
        Re: Copy array with small changes Keith Thompson <kst-u@mib.org> - 2018-03-12 09:08 -0700
          Re: Copy array with small changes supercat@casperkitty.com - 2018-03-12 09:57 -0700
            Re: Copy array with small changes Richard Damon <Richard@Damon-Family.org> - 2018-03-13 13:21 -0400
    Re: Copy array with small changes Noob <root@127.0.0.1> - 2018-03-12 15:25 +0100
      Re: Copy array with small changes Spiros Bousbouras <spibou@gmail.com> - 2018-03-12 15:09 +0000
  Re: Copy array with small changes Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-03-12 06:59 +0000
    Re: Copy array with small changes Noob <root@127.0.0.1> - 2018-03-12 15:55 +0100
      Re: Copy array with small changes bartc <bc@freeuk.com> - 2018-03-12 15:07 +0000
      Re: Copy array with small changes mark.bluemel@gmail.com - 2018-03-12 09:03 -0700
      Re: Copy array with small changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-03-12 17:42 +0000
        Re: Copy array with small changes Noob <root@127.0.0.1> - 2018-03-12 23:33 +0100
          Re: Copy array with small changes Wouter Verhelst <w@uter.be> - 2018-03-12 23:44 +0100
          Re: Copy array with small changes Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-17 09:48 -0700
            Re: Copy array with small changes Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-18 03:31 -0700
            Re: Copy array with small changes Noob <root@127.0.0.1> - 2018-03-22 17:10 +0100
        Re: Copy array with small changes Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-17 09:24 -0700
          Re: Copy array with small changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-03-17 18:04 +0000
            Re: Copy array with small changes Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-26 22:55 -0700
              Re: Copy array with small changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-03-27 15:28 +0100
                Re: Copy array with small changes Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-28 14:42 -0700
      Re: Copy array with small changes Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-03-13 12:46 +0000
  Re: Copy array with small changes Anton Shepelev <anton.txt@gmail.com> - 2018-03-12 22:36 +0300
  Re: Copy array with small changes Wouter Verhelst <w@uter.be> - 2018-03-12 23:32 +0100
    Re: Copy array with small changes Wouter Verhelst <w@uter.be> - 2018-03-13 10:42 +0100
    Re: Copy array with small changes Noob <root@127.0.0.1> - 2018-03-13 11:37 +0100
      Re: Copy array with small changes Wouter Verhelst <w@uter.be> - 2018-03-13 21:27 +0100
  Re: Copy array with small changes Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-17 09:55 -0700

csiph-web