+ 14
Difference between some functions and methods.
There are some functions/methods which do the the same work as other ones do. like, variable.sort() and sorted(variable) again, >>> 3**3 and from math import pow print(pow(3,3)) Do they have some advantages over anothers?? Please inform if you know about some more pairs which do the same work.
10 odpowiedzi
+ 6
import timeit
from math import pow
start_time=timeit.default_timer()
print(3**3)
print(timeit.default_timer()-start_time)
start_time=timeit.default_timer()
print(int(pow(3,3)))
print(timeit.default_timer()-start_time)
a=[1,2,4,2]
start_time=timeit.default_timer()
print(sorted(a))
print(timeit.default_timer()-start_time)
a=[1,2,4,2]
start_time=timeit.default_timer()
a.sort()
print(a)
print(timeit.default_timer()-start_time)
+ 9
Aymane Boukrouh [EXAMS]
I did not ask for the diff. between 'function' and method.
I asked for the diff. between relatively same fun. and methodes which outputs almost same.
like the second example
they output same all the time.
+ 7
OLABAMIDELE FEMI
please post your question as a que. in the discussion feed with the code you written(your trial)
+ 6
There are multiple functions that do the same work, and the advantages vary depending in the project you want to make. However, for small codes, it shouldn't matter, because after all it's just different names for the same thing.
Note that variable.sort() and sorted(variable) are NOT the same thing. variable.sort() changes the variable itself, while sorted(variable) only return a sorted version of it, but doesn't affect the variable.
EDIT: I just noticed you were asking for functions/methods and not different functions with same output. Your second example is confusing :/
This question has been asked many times before, please use the searchbar before asking next time please.
+ 3
Yes, but if knowing the fundamental difference between the two types will justify the use of each one of them.
Both of the examples you gave are not really related to your question, because in the first example the function and method are different, and in the second one there is no clear usage of method, so you might want to give better examples.
However if they output the same result, then the only difference would be speed, and as I mentioned earlier speed is not very important for small codes.
There is no global definition to which is faster, so google is your friend if you want to know which one fits better.
+ 3
Paste the code and you will see that 3**3 and sort takes less time than the other
+ 2
https://www.sololearn.com/discuss/1162147/?ref=app
https://www.sololearn.com/discuss/246039/?ref=app
https://www.sololearn.com/discuss/1306327/?ref=app
https://www.sololearn.com/discuss/1438962/?ref=app
https://www.sololearn.com/discuss/677538/?ref=app
https://www.sololearn.com/discuss/2184568/?ref=app
https://www.sololearn.com/discuss/249/?ref=app
https://www.sololearn.com/discuss/58523/?ref=app
https://www.sololearn.com/discuss/1913121/?ref=app
https://www.sololearn.com/discuss/30991/?ref=app
+ 2
Not sure if this is any help..but paste it into your ide:
import timeit
print(timeit.timeit(stmt="mylist.sort()", setup="mylist = [10, 3, 5, 4, 7, 9, 8, 6, 2, 1]"))
print(timeit.timeit(stmt="list2 = sorted(mylist)", setup="mylist = [10, 3, 5, 4, 7, 9, 8, 6, 2, 1]"))
print(timeit.timeit(stmt="3**3"))
print(timeit.timeit(stmt="math.pow(3,3)", setup="import math"))
+ 1
If I understand your question correctly : at times, functions may use less CPU time than methods, but only some times. If execution speed is an issue, you can setup timers and compare which will be faster in your case