Adding very large arrays I am writing a stochastic simulation for a Yule process:

If you start with a certain number of bins each containing a random number of balls, you add another ball to an existing bin with a probability that is proportional to the number of balls within that bin, as well as create another bin with a single ball each time step.

Because I am trying to basically weed out any stochastic noise, I am using very large number of iterations, on the order of 1e6. This code was originally developed in MATLAB, making much less complicated, but is far too slow for large simulations.

Is there a better way of doing this?

#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

void init_KISS();
unsigned int JKISS();
double uni_dblflt();
double array_sum();

int main()
{
    int t,count,i,j,k,ii,jj,kk;
    int desval;
    desval=1000000;
    unsigned int r;
    double v,u,bsum;
    init_KISS();    
    for(i=0; i&lt;100000;i++)
    {
        r = JKISS(); /* waste the first 100000 random numbers */
        uni_dblflt();
    }
    /*allocate memory for all params*/
    double *bins=malloc((desval+1)*sizeof(double));/*Urns, keeps track of actual "balls" in each bin*/
    if(bins==NULL)
    {
        printf("out of memory\n");
        exit(1);
    }

    double *p=malloc((desval+1)*sizeof(double));/* Urns/(sum of all "balls) i.e. the probability
                                                 that a new ball get placed in the ith urn. */
    if(p==NULL)
    {
        printf("out of memory\n");
        exit(1);
    }

    double *ptot=malloc((desval+1)*sizeof(double));/*Sum of all probabilities for the 1st to the ith
                                                    bin*/
    if(ptot==NULL)
    {
        printf("out of memory\n");
        exit(1);
    }

    for(i=0;i&lt;(desval+1);i++)
    {
        bins[i]=0;
        ptot[i]=0;
        p[i]=0;
    }

    for(i=0;i&lt;10;i++)/*seed 10 initial bins with 1 "ball"*/
    {
        v=1;
        bins[i]=v;
    }


    for(j=10;j&lt;desval;j++)
    {
        bsum=array_sum(bins,desval);/*sum the balls in each bin*/
        for(k=0;k&lt;j;k++) /* calculate the probability that with which a ball might be placed in 
                          each urn*/
        {
            p[k]=bins[k]/bsum;
        }

        for(ii=0;ii&lt;desval;ii++)/*Evaluate the probability total cutoffs*/
        {
            ptot[ii]=array_sum(p,ii);
        }
        u=uni_dblflt();
        count=0;
        while(u&lt;=ptot[count])/*choose bin to place new ball. The numerical value of ptot(count) 
                              is directly related to the probability space with which u can be 
                              picked*/
        {
            count++;
        }

        bins[count]++;
        bins[j]=1;
    }

    for(jj=0;jj&lt;desval;jj++)
    {
        printf("%.8f\n",bins[jj]);
    }

    free(bins);
    free(p);
    free(ptot);
    bins=NULL;
    p=NULL;
    ptot=NULL;
    return 0;
}

double array_sum(double *arr, int maxval)
{
    /* this function sums a large array*/
    int i;
    double sum; 
    sum=0;

    for(i=0; i&lt;maxval;i++)
    {
        sum +=arr[i];
    }

    return sum;
}
This is really an algorithms question. For example, consider this loop:

for(ii=0;ii&lt;desval;ii++)/*Evaluate the probability total cutoffs*/
{
    ptot[ii]=array_sum(p,ii);
}


why are you calling array_sum() every time through this loop when the only thing that changes is the addition of one more element?
Why can't this loop be: 

sum= 0;
ptot[0]= 0;
for(ii=1;ii&lt;desval;ii++)/*Evaluate the probability total cutoffs*/
{
    ptot[ii]=(sum+= p[ii - 1]);
}