c++ - initializing an array of constants in a struct -


to stack overflow members, coding simulator requires multiple loops (sometimes on 1,000,000) each involves heavy calculations. thus, saving 10 millisecond on loop can lead saving on 160 minutes of calculations. fighting every code optimization can get. had imported system parameters file vectors. after awhile realized that:

  1. calling constant int/double/... faster calling regular int/double/...
  2. using arrays faster using vectors
  3. i did not need full functionality of std::string , make simple knockoff version of use

originally naively thought convert std::vector<double> std::vector<const double>. i've been trying make cross-platform linux found visual studio express compiler ignores fact const can't declared (and linux throws ton of errors).

i decided use constant arrays in structs initializing; however, not figure out how initialize properly. (most/all forums came across said declare static const int* , initialize global, don't think works need). developed final solution; however, believe committed 'taboo' ended using pointer declare constant. compiles , runs no errors , wondering, wrong initialize constant array using pointers? , if so, how else it? (i understand might slower data way, in part time matters should faster call... think)

#include <stdio.h> #include <malloc.h> #include <conio.h>  #if defined(_win32) || defined(_win64) #define gsize _msize #else #define gsize malloc_usable_size #endif  struct testy{     const int *i;     const double *d;     testy(){         i=(const int *)malloc(0);         d=(const double *)malloc(0);     } };  inline const int* getdat(const int* dst, int* src){     dst=(const int*)realloc((void *)dst,gsize((void *)src)); // allocate enough memory hold data     return src; }  inline const double* getdat(const double* dst, double* src){     dst=(const double*)realloc((void *)dst,gsize((void *)src)); // allocate enough memory hold data     return src; }  int main(){     testy data;     int *tmp_i = (int *)malloc(0);     double *tmp_d = (double *)malloc(0);      for(int i=0;i<3;i++){ // load empty array want         tmp_i=(int*)realloc((void *)tmp_i,gsize((void *)tmp_i)+1*sizeof(int)); // increase size 1         tmp_i[i]=i;     }     for(int i=0;i<3;i++){ // load empty array want         tmp_d=(double*)realloc((void *)tmp_d,gsize((void *)tmp_d)+1*sizeof(double)); // increase size 1         tmp_d[i]=i;     }      data.i=getdat(data.i,tmp_i);     data.d=getdat(data.d,tmp_d);      printf("\nintegers\n");     for(int i=0;i<(gsize((void *)data.i)/sizeof(int));i++)         printf("%d\t",data.i[i]);      printf("\ndoubles\n");     for(int i=0;i<(gsize((void *)data.d)/sizeof(double));i++)         printf("%lg\t",data.d[i]);      free(tmp_i); free(tmp_d);     _getch();     return 0; } 

i removed knockoff string , uses code there issues declaring array of , didn't want ask more 1 question here.

  1. remove realloc. expensive , fragments memory
  2. const not make difference - have access memory @ end of day , const bit ensures not alter things shouldn't
  3. place things (gsize((void *)data.d)/sizeof(double) outside loop if value immutable. prevents function call each time around loop
  4. make memory cache work. i.e. access stuff in memory sequentially. data shipped memory processor can pre-fetched easily.
  5. as doing complex operations (that have not shown) use little algebra simplify equations. if possible use integers on doubles if possible, depends on problem space.

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

node.js - Node - Passport Auth - Authed Post Route hangs on form submission -

Does Firefox offer AppleScript support to get URL of windows? -