For a set of 2D or 3D points \$\{\mathbf P_0, \mathbf P_1,\cdots,\mathbf P_n\}\$, their parameters \$\mathbf T = \{t_0,t_1,\cdots,t_n \}\$ could be computed as follow:$$\begin{align}t_0&=0\\t_i&=\frac{\sum_{j=1}^{i}||\mathbf P_j- \mathbf P_{j-1}||}{\sum_{j=1}^{n}||\mathbf P_j-\mathbf P_{j-1}||} \qquad i=1,2,\cdots,n\end{align}$$Here is my C implementation:/********************************************************************         v1 = {v1(0),v1(1),...,v1(c-1)}           v2 = {v2(0),v2(1),...,v2(c-1)}     compute the 2-norm of the difference of two vector v1 and v2*********************************************************************/double vector_norm(double *v1, double *v2, int c) {    int i;    double res = 0.0;    for (i = 0; i < c; i++) {        res += pow(v1[i] - v2[i], 2);    }    return sqrt(res);}/******************************************************** In general, P is a matrix of dimensions {n + 1, c} here, it was saved as a vector********************************************************/void calc_paras(double *P, int n, int c, double *paras) {    int i;    double len = 0.0;    double chord;    //compute the length and total length    paras[0] = 0.0;    for (i = 0; i < n; i++) {        chord = vector_norm(P + (i + 1) * c, P + i * c, c);        len += chord;        paras[i+1] = len;    }    //normalize the paras to [0,1]    for (i = 0; i <= n; i++) {        paras[i] /= len;    }}Is a more efficient C implemetation of calc_paras() possible?
Parametrization of a curve
performance;c;computational geometry
Is a more efficient C implementation of calc_paras() possible?Some small ideas:Use const.  A compiler may employ certain optimizations knowing the pointers are to const data.// double vector_norm(double *v1, double *v2, int c) {double vector_norm(const double *v1, const double *v2, int c) {If the pointers never overlap, let the compiler know that and allow it additional optimizations using restrict.// double vector_norm(double *v1, double *v2, int c) {double vector_norm(const double * restrict v1, const double * restrict v2, int c) {pow(x,2) is questionable.  Profile against res += (v1[i] - v2[i])*(v1[i] - v2[i]);Unclear why code is using int vs. size_t to denote the size of a vector.   size_t is more C-like.Minor. Might as well avoid the +1paras[0] = 0.0;for (i = 1; i <= n; i++) {  chord = vector_norm(P + i*c, P + (i-1)* c, c);  len += chord;  paras[i] = len;}If vector_norm() is only meant as a helper function for calc_paras(), make vector_norm() static to un-clutter the name space.