Блог пользователя alive_ak4c

Автор alive_ak4c, история, 19 месяцев назад, По-английски

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.

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
19 месяцев назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

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

»
19 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 ..

  • »
    »
    19 месяцев назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    Oh, any examples?

    • »
      »
      »
      19 месяцев назад, # ^ |
        Проголосовать: нравится +15 Проголосовать: не нравится

      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.

      • »
        »
        »
        »
        19 месяцев назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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()