Function Called With Same Arguments Again & Again

This is a discussion on Function Called With Same Arguments Again & Again within the C++ Programming forums, part of the General Programming Boards category; My algorithm uses a function like ....... insert Code: long double E(int x1, int x2, int x3, int x4, int ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    1

    Function Called With Same Arguments Again & Again

    My algorithm uses a function like .......
    insert
    Code:
    long double E(int x1, int x2, int x3, int x4, int x5)
    // x1..x5 start from 1 and go upto few thousand (2,500)
    {
    decoding of x1,x2,x3,x4,x5 takes place ...... 
    and parmeters are chosen from the problem data set 
    
    for(int l=1; l<5; l++)
    for(int i=1; i<5; i++)
    {
    
    case 1:
    long doubel f1( parameters of xi )
    {
    long double g1( parameters of xi ) ;
    long double h1( parameters of xi ) ;
    ................
    }
    case 2:
    long doubel f2( parameters of xi )
    {
    long double g2( parameters of xi ) ;
    long double h2( parameters of xi ) ;
    ................
    }
    case 3:
    long doubel f1( parameters of xi )
    {
    long double g1( parameters of xi ) ;
    long double h1( parameters of xi ) ;
    ................
    }
    }
    
    }
    that is the function is complex and involves further calls of other funtions



    A number of sets {x1,x2,...,xn} are produced during the program run for which the function E() has to be evaluated. The problem is that a set of values of {x1,x2,...,xn} may repeat itself again & again. And it is unnecessary to compute the function value for the same set of values as the value will be the same with only the additional computation time. And computation time has to be minimized for efficient programming.

    How can i do this ..............


    Somebody told me that hashing can be used here ..... but i don't know whether he is right or not as i have never done hashing ......... it is not known to me .....

    Can anybody suggest me a simple way to avoid calling the same function for duplicate sets of values ..... in order to save computation time.?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would use a hashing algorithm to determine if x1, x2, x3, x4 and x5 are the same as some previous set.

    The problem is coming up with a good hash-algorithm. Are all the values in the range 0 .. 2500 used equally, or are some value(range)s more common than others?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Well, it also depends on what calculating the values involves, but you could keep a record for each set of the calculated value adn a flag as to whether it has been previously computed. This is how I woudl do it -

    Code:
     
    class MySet {
    public:
       MySet(int members);
       ~MySet();
     
       BOOL Calculated;
       double ReturnValue;
     
       double* TheData;
       } ASet;
     
    MySet::MySet(int members){
       this->Calculated = FALSE;
       this->Members = members;
       this->TheData = (double*)malloc(members * sizeof(double));
       return;
       }
     
    MySet::~MySet(){
       if(this->TheData != NULL) free(this->TheData);
       return;
       }
     
     
    void main(void){ // kisses sweety :)
       double Foo;
     
       MySet* Data = new MySet(10);
     
       Foo = SomeFunc(Data);
       return;
       }
     
    double SomeFunc(MySet* pSet){
       if(pSet->Calculated) return pSet->ReturnValue;
     
       // actually do the calculation
       pSet->ReturnValue = (your cacluation);
       
       pSet->Calculated = TRUE;
     
       return pSet->ReturnValue;
       }
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But you would need a set of 2500 ^ 5. With 11102 TB, it may be a little bit out of reach for most of us.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Have you looked into using C++ Maps? Here's a reference link http://www.cppreference.com/cppmap/index.html.

    Use the x as the key and use the result of your computation as the value if you need to.

    HTH

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pantherse View Post
    Have you looked into using C++ Maps? Here's a reference link http://www.cppreference.com/cppmap/index.html.

    Use the x as the key and use the result of your computation as the value if you need to.

    HTH
    You would of course have to take into account that there are 5 values of x that need to be combined somehow. My idea would be to generate a string of 20 characters that is filled with the values of the x1..x5 [with space or zero-padding to make each elemen the same length].

    But that still assumes that there's a great sparseness in the values used - if all values for all positions are used, then the table would be ginormous, and not work out very well.

    If there is a huge number of actively used options, then using some sort of cache to hold the last X number of values, and if we find a repeat, then use the cached value, if not, then just add the latest value to the cache (and if the cache is full, remove some other cache-entry).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 9
    Last Post: 01-02-2007, 04:22 PM
  3. Arrays as function arguments :(
    By strokebow in forum C Programming
    Replies: 10
    Last Post: 11-18-2006, 03:26 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21