safarisoul's blog

By safarisoul, history, 10 years ago, In English

Intuitively, long as primitive type should be faster in case of multiplication or other arithmetic operations than Long as object type. However, my recent submissions to a CF problem 439B - Деву-дурачок disproved this intuition.

15307785 using long, the primitive type got TLE, whilst 15307798 using Long, the object type got Accepted, and has a large timely improvement over the previous one by like 10 times.

Does anyone have any idea why this happened?

Here is another problem :285C - Получаем перестановку

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

»
10 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by safarisoul (previous revision, new revision, compare).

»
10 years ago, hide # |
Rev. 3  
Vote: I like it +8 Vote: I do not like it

Primitive types use a type of sort which can go to O(N^2) in the worst case. ( I think it's quick sort).

Wrapper classes ( Objects ) for Primitive types use an O(NLogN) worst case running time algorithm for sorting. ( Merge sort ).

It has nothing to do with the Object (Wrapper class ) being faster. It's mainly because of sorting.

Before sorting, shuffle your array for the "long" submission, and you will get AC.

UPDATE: Here is AC with "long" in 156 MS and faster than Long.

http://mirror.codeforces.com/contest/439/submission/15340014

»
10 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by safarisoul (previous revision, new revision, compare).