Ask coding questions

← Back to all posts
Do methods take more time to run?
MihirMalaviya

Do methods take more time to run?
Example: I have a method that takes a string input and prints it in a println statement. Now, when this is called, will it take longer to run? What if I run it 100 times, will there be a difference in the time? Does it scale exponentially? Is said difference negligible for such a small method? What if I make a more complicated method?

PS: This is my first post, so sorry if I did anything wrong, I just thought this question would help me, and others, thanks for your time!

Answered by SixBeeps [earned 5 cycles]
View Answer
Voters
MattDESTROYER
MihirMalaviya
Comments
hotnewtop
SixBeeps

Hi,

One of the first things a Computer Science student learns is what's called Big-O notation. Big-O describes the graph of how many operations are performed given a certain algorithm. If an algorithm is O(log n), it increases logarithmically, whereas if it's O(n³), it grows cubically.

In your case, your method is O(n) where n is the number of times you call it. This does not mean that the number of times you call it and the number of operations it does is exactly 1:1, but it does tell us that it is linear.

If you reeeeally want to know why, let me explain briefly what your function does in the compiler/interpreter. Before, our function looks something like this in the code (assuming you're using an object-oriented language):

It looks like only one operation is performed here. But, what the system does is close to this:

  • Load message into memory slot 1
  • Load memory slot 1 into the parameters stack*
  • Initiate a system call titled "print"
  • Go back in the call stack

That's four whole operations! So the total number of operations is 4n, where n is the number of times you call the function. At 100 times, that's 400 operations.

If you were to not use a function at all and instead call it separately, you'd get rid of the fourth operation, effectively reducing 400 operations to just 300. This sounds like a huge performance increase, but you have to realize that computers are super fast nowadays, so the difference is negligible. Honestly, the largest timesucker here is printing, which is a slow process on its own.


tl;dr, Using a method is slower, but not by a considerable amount.

* This is not at all accurate iirc, but it makes sense to me for some reason.

DynamicSquid

@SixBeeps great answer!

MihirMalaviya

@SixBeeps This was very helpful, thanks!

Odobasa2wyoming

Thank you so much! drift boss

Coder100

yeah of course it takes time for the user to do input.

It can't be instantaneous, computers can only do one thing at a time in a thread. So that means it will linearly increase. Any use case?

Carnage498

Unless you specifically order the program to change the timing, it should take its average time. Btw on a problem with the post. But if your new you should read the rules for the posting. Average time = around 0.5 - 1 sec. No matter what the method is, it will take its average time. Yes some method's may take more time(the complication of the method does not matter)

If your question is answered you can check it and it tells that this question has been answered. It also gives cycles, but there not important.

PS: here's the link to the "repltalk rules":

https://replit.com/talk/announcements/Repl-Talk-Rules-and-Guidelines-README/135633

Hope it helps! ~ @Carnage498