Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #127666
| From | Spiros Bousbouras <spibou@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Copy array with small changes |
| References | <p848dh$oko$1@dont-email.me> <qShpC.133290$Zg.23093@fx21.am4> <SJspC.109257$qj4.65637@fx37.am4> |
| Message-ID | <NvupC.120298$856.70911@fx42.am4> (permalink) |
| Organization | virginmedia.com |
| Date | 2018-03-12 12:37 +0000 |
On Mon, 12 Mar 2018 10:35:32 +0000 bartc <bc@freeuk.com> wrote: > On 11/03/2018 22:13, Spiros Bousbouras wrote: > > On Sun, 11 Mar 2018 22:51:40 +0100 > > Noob <root@127.0.0.1> 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) > > > > void update_array(const int * const arr , const size_t n , > > const size_t zi , const size_t zj , const int val) > > > > Actually something like arrsz is a better name than n. > > No, this suggests array size which in C usually means byte-size, not > number of elements. You have any statistics to back this up ? I always use it to mean number of elements. > You might have pointed out that 'update-array' is > misnamed as the array passed is not modified. Sure. > >But since the > > function is short it doesn't matter that much. > > Well, it /had/ been short until you introduced all these consts! (Is > there really a benefit from declaring individual int parameters as const?) It remains short. I thought the const declarations might help with optimisation but the only way to tell is to test. > Also, wasn't the OP requesting something less ugly? And something a bit > faster? Stipulating possible 64-bit types for n, zi and zj, when it was > stated that all these will be less than 12, probably won't help. size_t is the canonical way to refer to array sizes and array indexes ; anything else gives one more to think and worry about (like type conversions) in an area where dangerous errors happen most often. So size_t is the default option unless one has good reasons to do otherwise. If on a machine size_t is 64 bit then almost certainly it will be a 64 bit processor so 32 bit integer operations aren't going to be faster than 64 bit. In any case the opening poster can test ; if using int achieves faster speed than using size_t and it makes a difference to him then this would be a good reason to prefer int over size_t . -- vlaho.ninja/prog
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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