pallav12's blog

By pallav12, history, 3 years ago, In English

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?

  • Vote: I like it
  • -7
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

»
3 years ago, # |
  Vote: I like it +1 Vote: I do not like it

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.

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

    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.

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

      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.

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

      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.

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

        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.