alive_ak4c's blog

By alive_ak4c, history, 2 years ago, In English

Recently I was solving this question in python. I had thought of a solution and I tried submitting it in python 3, although most of my solutions were accepted via python 3 interpreter. This problem held me by TLEs, a bunch of them.

I was so confused that I started breaking the logic in further if else's only to find that they don't help. I started to search for reasons on the internet and stumbled upon this article just by luck.

It is a TLE saviour. I skimmed through the article and submitted my code in pypy 3 instead of python 3 and to my surprise, it got Accepted. My amusement didn't end here. The execution time was very much comparable to the c++ code and almost 5 times faster than python 3.

So I am switching to this interpreter now. And if you too are getting TLEs it is would be worthy to try submitting your code with pypy 3.

Happy Coding!!!

Edit1:

Looks like pajenegod has a valid point here, this is not always true.

My apologies!!!

Edit 2:

Codeforces submission page also recommends using PyPy. Proof

So, it's a recommendation rather than a guarantee.

  • Vote: I like it
  • +6
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
  Vote: I like it +6 Vote: I do not like it

It Will be more better if You will switch to C++.

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Ya pypy3 saves u from tle but sometimes even pypy3 will give u tle and python3 will give u ac like in a question involving dfs or string manipulation stuff ..

  • »
    »
    2 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Oh, any examples?

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it +15 Vote: I do not like it

      There is no guarantee that PyPy is faster than CPython. PyPy's JIT is really frail and can sometimes mess up big time. A basic example would be something like this

      def f():
          for i in range(20000):
              for j in range(1041):
                  i == 0
                  j % 6 == 0
      f()
      

      This runs in 3 s with PyPY3, and 2 s with CPython3. But if you change j%6 to j%5 PyPy3 runs in 0.1 s.

      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Yet another proof why PyPy's JIT is confusing as f**k: that exact code runs faster when you add a stupid loop doing nothing. try the following code.

        def f():
         for i in range(20000):
          for j in range(1041):
           i == 0
           j % 6 == 0
           for _ in [0]:pass
        f()