yash_0402's blog

By yash_0402, history, 6 weeks ago, In English

After seeing this, almost everyone will submit the code in PyPy.

Now checkout this two solutions : 104868543 and 104820924

Both of them has same code and it was the solution of A problem of div2. The time complexity of the code was O(n). PyPy code gave TLE and Python code gave AC resulted in penalty of 50 points.

It's not over yet. Same thing happened in Problem C of same contest, but this time Python code gave TLE and PyPy code worked fine. Python code : 104867840 and PyPy code : 104868496

Now how one will decide to submit solution in PyPy or Python. Or every time we have to suffer a penalty of 50 points by trying in both.

Please some one look into it and help out Python community

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

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

The second time, some lines were slightly different, but a factor of at least 10 with the first example is just insane. 93ms vs >1000ms. My honest opinion would be to use something other than Python except for the first problem, just because it's slower. Still, I say this as a Python programmer who has submitted several solutions in Python. It's quite a bit easier to work with in many cases, and it's one of the most popular languages too, so I really think this should be looked into.

One theory I have is that the PyPy optimizations make some operations a lot faster (obviously), but they make other operations slower, and maybe the slower operations were used much more in A than in C, so that's what makes up the huge difference. Maybe the == and/or the += operators are slower, but that doesn't seem to have any kind of explanation as to why.

»
6 weeks ago, # |
  Vote: I like it +24 Vote: I do not like it

Python in general is slower than Pypy. But there are some places where Pypy and Python work differently.

In the first problem, you are appending a character into a string repeatedly. This process is O(1) in Python , But in Pypy this process takes O(|s|) every time. There are also few other differences between them. Sets and dictionaries are very slow in python when compared to Pypy. Try writing small snippets of code and running them in custom invocation.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Thanks rishabnahar2025 I have checked out the previous instances when I faced similar problem, and I found out they were also because of appending in string.

    Can you share some reference from where I can check out which thing works faster in PyPy and Python.

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Instead of appending to a string, append to a list. It will be faster that way

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it -12 Vote: I do not like it

      Have a look at this website: https://doc.pypy.org/en/latest/cpython_differences.html

      Pypy has mentioned all the differences but most of them are not really gonna affect you. I'll mention a few differences that I noticed

      ar=[1,2,3,4] temp=ar.copy() print(temp)

      Runs in Python In pypy2-->runtime error , list object has no attribute copy(use list(ar) to copy a list)

      Sets and Dictionaries are very slow in Python

      • »
        »
        »
        »
        6 weeks ago, # ^ |
          Vote: I like it +30 Vote: I do not like it

        I've tried telling you before

        .copy for lists only got added in Python 3, so of course it is not going to work in PyPy2. It works perfectly fine in PyPy3.

»
6 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

I also faced the same issues with string concatenation in pypy. You can use an array and append characters to it which is O(1) and then use join method at the end. 104778493

»
6 weeks ago, # |
  Vote: I like it -25 Vote: I do not like it

What admins can do? That's not a codeforces problem, you decided to programming on python, so that's only your problem.

Otherwise, codeforces need different time limit for different languages, but I think its stupid.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it +20 Vote: I do not like it

    First of all, I have not tagged any admins here, I asked for community help. Title of the blog also justifies it, it's Python vs PyPy , not something like "Justice to python coder".

    Second I didn't asked for different time limit but how its 'stupid'? CP is all about problem solving skills and any language should not be unfavourable.

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it -14 Vote: I do not like it

      In my opinion, CP is about getting AC, so if you solve problem in your mind and cant get AC, u dont solve it

      • »
        »
        »
        »
        6 weeks ago, # ^ |
          Vote: I like it +4 Vote: I do not like it

        I think you should take some rest rather than talking out of context. This AC mentally only increasing plagiarism day by day.

»
6 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

So i found that string concat. is slower in pypy you cannot get AC in problem A even with fastest io 104872125, use list instead 104784263. What are other slower things in pypy3 ? HELP pajenegod, conqueror_of_tourist

»
13 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Come here to share another example. I recently encountered 1480B. The time complexity is O(n). My PyPy code gave TLE while my Python code gave AC.

It's truly an annoying problem. In view of this, I am considering switching to lower level programming language like C/C++ or Rust. Good news is to some extent we can simulate Python-style code in C/C++ with the aid of macro magic. Allusion to Having fun abusing the C language and Let's Destroy C.