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

Автор yash_0402, история, 4 года назад, По-английски

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

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

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

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.

»
4 года назад, # |
  Проголосовать: нравится +24 Проголосовать: не нравится

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.

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

    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.

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

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

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится -12 Проголосовать: не нравится

      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

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

        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.

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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

»
4 года назад, # |
  Проголосовать: нравится -25 Проголосовать: не нравится

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.

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

    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.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится -14 Проголосовать: не нравится

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

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

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

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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