Ask your own question, for FREE!
Computer Science 24 Online
OpenStudy (anonymous):

Should I always use library functions over stuff I wrote myself?

OpenStudy (jamesj):

Nearly always because they'll be optimized (or close to it).

OpenStudy (anonymous):

That's the idea Andrew Ng (ML class instructor) was talking about; try to use some powerful linear algebra library, if available, to do the tasks.

OpenStudy (anonymous):

I think there are nice AI libraries for Python out there.

OpenStudy (anonymous):

as well as numpy.

OpenStudy (jamesj):

Also, if your language is not compiled as a separate step before being run, then it always faster to use library functions, because they have been compiled into machine/assembly language and that will be faster than any code you write which will have to be interpreted before being run.

OpenStudy (anonymous):

but in python I timed from math import factorial factorial (bignumber) and something like def fact(x): ans = 1 n = 2 while n <= x ans *= n n += 1 return ans fact(bignumber) and they run almost the same, although the second one usually runs a few milliseconds faster

OpenStudy (jamesj):

In which case--if that's an important function in your program--use yours. But in general, the functions are more complicated and have been optimized.

OpenStudy (anonymous):

I'm sure that library factorial function also does some fancy stuff behind the scenes that makes it better" http://svn.python.org/view/python/trunk/Modules/mathmodule.c?view=markup (line 1088)

OpenStudy (stormfire1):

The other thing to consider here is that library functions are usually thoroughly unit-tested. The more functions you write yourself...the more unit tests you'll need to write & debug to test your code.

OpenStudy (anonymous):

I'll play devil's advocate. Sometimes APIs don't give you fine-enough granularity for what you want to do, so you'll end up implementing some things for yourself. Sometimes libraries are bloated with features you don't need, and you don't want to distribute a huge 3rd-party library for one or two functions. Sometimes libraries have bugs, or aren't implemented well. Some libraries can be expensive to use, and more expensive to redistribute. Some libraries have labyrithine dependencies that I'd rather not have to deal with (using java hibernate on a small project is such a PITR). Sometimes writing it yourself is more fun, and more educational. Etc.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!