[PYTHON] It became TLE when I confirmed the operation with the print function in the competition pro

It became TLE when I confirmed the operation with the print function in the competition pro

example

I actually noticed this in the following AtCoder problem: https://atcoder.jp/contests/abc162/tasks/abc162_c I was turning a 200 * 200 * 200 for statement.

problem

The output was done with the print function to check the operation.

Code in question

I am using the time module for time measurement.

import time
import math
k=int(input())
stat = time.time()
k+=1
ans=0
for i in range(1,k):
    for j in range(1,k):
        for z in range(1,k):
            print(i,j,k)
            print(math.gcd(math.gcd(i,j),z))
            ans+=math.gcd(math.gcd(i,j),z)
end = time.time()
print(ans)
print(end-stat) 
##k=10  =>  0.0718231201171875(s)
##k=100 => 20.816463232040405(s)

Code without print function

import math
import time
k=int(input())
start = time.time()
k+=1
ans=0
for i in range(1,k):
    for j in range(1,k):
        for z in range(1,k):
            ans+=math.gcd(math.gcd(i,j),z)
end = time.time()
print(ans)
print(end-start)
#k=10  => 0.0009169578552246094(s)
#k=100 => 0.4900369644165039(s)

Conclusion

It seems that it takes a lot of time if print also overlaps. First try it while printing a little (k = 10 this time) You must comment out when calculating time-consuming orders.

Recommended Posts

It became TLE when I confirmed the operation with the print function in the competition pro
What I did when I was angry to put it in with the enable-shared option
I set the environment variable with Docker and displayed it in Python
Behavior when returning in the with block
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
[Python] I want to know the variables in the function when an error occurs!
When I tried to change the root password with ansible, I couldn't access it.
What I did when I got stuck in the time limit with lambda python
I referred to it when I got stuck in the django geodjango tutorial (editing)
When to_csv with Pandas, it became line by line
When I try matplotlib in Python, it says'cairo.Context'
I implemented the inverse gamma function in python
When I checked the query generated by Django, it was issued in large numbers
When I crawl the webapi that appears during rendering, it was played with CORS
When a local variable with the same name as a global variable is defined in the function
I asked the problem of the tribonacci sequence in C ++ & the number of function calls when writing with the recurrence function (python is also available)
The value of meta when specifying a function with no return value in Dask dataframe apply
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
I also tried to imitate the function monad and State monad with a generator in Python
When I calculated the similar words of careful + brave with word2vec, it felt unexpectedly valid
The story I was addicted to when I specified nil as a function argument in Go