Kr_Shubham's blog

By Kr_Shubham, 3 years ago, In English

This code is taking around 1300 ms. (size of set shouldn't exceed more than 1 at any time, Since I am inserting same element every time)

Spoiler

But , After commenting the s.erase(), it just took 15 ms.

Spoiler

Can anyone please explain the reason behind this? Thanks in advance :)

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

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

my guess is that the compiler sees that the set isn't actually used for anything, so it's optimized out

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

    Can we somehow avoid these optimizations? It was the only one moment in my life when I thought to use keyword volatile. But after I did that, code refused to compile, and I don't understand why. Would be glad if someone explained why it is happening.

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

      Actually compiler gives errors on erase and insert functions, so I suspect existing versions of these function doesn't implemented for volatile case. So it's tempting me to think maybe there is no optimization at all?

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

I think it has to do with memory allocation (e.g., malloc(..)), which is quite an expensive operation.

Without erasing, only the first insertion causes allocation (RB-tree nodes and all), and subsequent insertions only require reads (if the operations are not optimized away). On the other hand, with erasing, every inserting invokes memory allocation. And 7 million times is a lot!