# Using functions slows codes

**URL:** <https://community.freefem.org/t/using-functions-slows-codes/2150>\
**Category:** General Discussion\
**Created:** [November 30, 2022, 8:22am UTC](https://community.freefem.org/t/using-functions-slows-codes/2150 "2022-11-30T08:22:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![miguelpicos](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@miguelpicos](https://community.freefem.org/u/miguelpicos)\
**Post date:** [November 30, 2022, 8:22am UTC](https://community.freefem.org/t/using-functions-slows-codes/2150/1 "2022-11-30T08:22:15Z")

</div>

Hello,

I was working on a project and I started to use functions to make the code simpler. As the computing times started to get high, I started to optimize the code. I found that the main wastage of time was calling functions, because if the code inside the function is just written in the place where you call the function, It is much faster. I wrote a minimal example to show what i mean:

```auto
func bool example1(int[int] & I){
	for(int i=0; i<I.n; i++) I(i) = i;
	return 0;
}
real[int] timer(2);
int repetitions = 100;
int length = 1e6;
for(int j=0; j<repetitions; j++){
    int[int] I(length);
    real time = clock();
    example1(I);
    timer(0) += clock()-time;
}
for(int j=0; j<repetitions; j++){
    int[int] I(length);
    real time = clock();
    for(int i=0; i<I.n; i++) I(i) = i;
    bool exa = 0;
    timer(1) += clock()-time;
}
timer /= repetitions;
cout<<"Mean of times per iteration with function call: "<<timer(0)<<endl;
cout<<"Mean of times per iteration without function call: "<<timer(1)<<endl;

```

My question is, am I doing something wrong? Is there a way to use functions whithout slowing the codes? Maybe I should imput the data into the function in another form (like in C++ you do by using pointers).

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [November 30, 2022, 8:29am UTC](https://community.freefem.org/t/using-functions-slows-codes/2150/2 "2022-11-30T08:29:28Z")

</div>

Maybe you could try using macros instead of functions (macros just copy a piece of code in the compilation process).  
Furthermore I think loops in FreeFem are generally slow. You could try to use implicit for loops (see documentation) which I think are faster.

---

<div class="post-metadata">

**Author:** ![miguelpicos](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@miguelpicos](https://community.freefem.org/u/miguelpicos)\
**Post date:** [November 30, 2022, 8:38am UTC](https://community.freefem.org/t/using-functions-slows-codes/2150/3 "2022-11-30T08:38:22Z")

</div>

Thank you for answering this quickly,

I will try to use macros. I also wanted to store the functions on an .idp, I assume I can also store Macros there?

Loops in the examples where only to do something inside the function to show the slow it brings. Anyway, thank you for the advice.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [November 30, 2022, 11:29am UTC](https://community.freefem.org/t/using-functions-slows-codes/2150/4 "2022-11-30T11:29:30Z")

</div>

Yes, you can also define the macros in different files. A lot of FreeFem utilities are defined in macros in [FreeFem-sources/idp at develop · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/tree/develop/idp)
