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

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

While trying to solve this, I made 2 submissions with only difference in using Array<Array<Int>> instead of Array<IntArray>.

  1. IntArray got AC in 546 ms
  2. Array got TLE with more then 3 sec

Can anyone please explain reason for this?

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

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

I'm not a Java developer at all, but I found out the answer in less than 30 seconds. Try googling please, the first StackOverflow link answers your question thoroughly.

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

I do not use Kotlin for competitive programming, but I think the TLE is because of the fact that Array is a collection of "Int" data type while IntArray is "int" data type (read up on it, "Int" is a completely different data type than "int" in Kotlin). I don't know what causes the difference in performance though. I can be wrong. Perhaps a contestant who uses Kotlin can help. It seems interesting. Gotta say, that is one nasty error.

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

    The difference between int and Int in Kotlin is like between using primitive int and new int in C++. In a nutshell, Int allocates the number on heap and stores the pointer to the number, while int stores just the number itself. That is also the reason why you (OP) are seeing that the TLE submission uses more memory.

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

      Yaa, makes sense thanks, actually I did searched bt just didn't thought such simple memory allocation would make code 6x slower, I thought it's some wierd collision that I'm not aware of.

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

      Um........I am not the OP. What makes you say that? I do not use Kotlin, apart from android stuff. I do not use it for competitive programming......and for good reason. But yeah, perhaps you are right about why "Int" is slower. Also, this makes "Int" data type null-able, and yeah, Kotlin has a freakish way to deal with Nulls, although we android devs love it.

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

        I did not mean you're the OP. I wanted to comment on your comment, not the post, because the first part of my comment is answering your question about 'I don't know what causes the difference in performance'. The last sentence is a conclusion and refers to the whole situation, hence commenting OP's submissions. If Codeforces UI allowed me to reply to both the comment and the post at once, I'd do that. Sadly we have a comment tree, not a comment DAG, so I had to choose where to reply.