0

Compared with pair, is there any performance loss for tuple with only 2 elements?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1899020
  • 13,167
  • 21
  • 79
  • 154
  • 6
    When you profiled it, what result did you get? – JBentley Mar 27 '13 at 19:36
  • 1
    Benchmark it. It should be exactly the same using a modern compiler. But if you want to be 100% sure, test it. – mfontanini Mar 27 '13 at 19:37
  • 5
    Use the right tool for the job. Pair is for mapping something to something else; e.g. for use in `std::map`. Tuple is for holding a set of completely unrelated values, e.g. so that a function can return multiple things. Perf wise they should be essentially identical. – Billy ONeal Mar 27 '13 at 19:38

2 Answers2

2

I very much doubt whatever system you are designing is going to grind to a halt because you chose to use tuple over pair. In fact, I'd be very surprised if the compiler generated anything more or less for a 2-value tuple than it would do for a pair as they're (almost) equivalent.

I can assure you that, if your application grinds to a halt and your PC begins to melt - it won't be due to your choice of tuple over pair, or vice-versa.

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
1

You would have to profile it. Besides, why risking extended use for something intended to be used with only two elements. Personal preference is not enough. Pair is easily usable with Maps, while tuple is a collection of its own.

Claudiordgz
  • 3,023
  • 1
  • 21
  • 48