Thanks for your explanation, Milan!
On Thursday, 19 February 2015 10:29:10 UTC-5, Milan Bouchet-Valat wrote: > > Le jeudi 19 février 2015 à 07:10 -0800, Roy Wang a écrit : > > I'd appreciate it if someone can help me out with two questions: > > > > I think this function is not a type-stable function. This is because > > the type of out depends on the input A. If it is type-stable, why? > No, it *is* type stable, precisely because the type of the output > depends only on the *type* of the input (or at least it seems it works > that way, but without more code it's hard to tell). Type-instability is > when the type of the output depends on the *value* of the input. > > > function myfunc(A) > > out=similar(A) > > > > #performs some computation that is then stored in out > > return out > > > > end > > > > > > > > I think this function is a type-stable function. If it isn't, why? > > function myfunc!(out,A) > > > > #performs some computation that mutates out > > return nothing > > > > end > > > > > > If my conclusions are correct, would you say it is better programming > > practice to use the second function as much as possible? > The second function is also type stable (AFAICT). The difference is that > it does not create a copy of the input, i.e. it works in place. This can > be better programming practice as it allocates less memory. The > convention in Julia is to provide both myfunc!() and a convenience > wrapper caller myfunc() which calls myfunc!(similar(A)). > > > Regards >
