-3

I would like to know if implementing my own Doubly Linked List is better or using stl list.h ? in terms of efficiency and what would be the advantages and disadvantages of both ? Thanks

  • 3
    Unless you NEED to do your own because the expected behavior is way too different from what list.h provide, it's most likely that list.h will always be faster as it has been here, reviewed, updated for years. Plus it's probably handling more errors than what you'll think about. – N.K May 14 '18 at 11:54
  • 2
    Since nobody knows how your version looks like, the answer is impossible to give. Please take some time to study the development of the STL, there are numerous documents online. – Ulrich Eckhardt May 14 '18 at 11:54
  • 3
    By `list.h` you mean `` right? In that case the standard list is 99% of times better. On the other hand in 99% of times you shouldn't use a linked list at all... – DeiDei May 14 '18 at 11:54
  • Implementing your own version is definitely better. It will teach you a lot more than just using it without knowing how it works. – stark May 14 '18 at 11:57
  • 1
    @stark You're talking in sense of the teaching experience. In that case, yes. The OP asks about efficiency which the standard library provides better. – DeiDei May 14 '18 at 11:59
  • My own version is simple , I just wanted to use it in a LRU Cache. so the application is quite simple – mawahballah May 14 '18 at 11:59
  • 1
    @mawahballah : You'll probably get better performance out of `std::vector`. Particularly if your cached object is movable. (`std::list` has *horrible* cache locality). – Martin Bonner supports Monica May 14 '18 at 12:19
  • 1
    "in terms of efficiency" - in terms of efficiency; it's better to not re-invent the wheel. That's why libraries exist. A comparable question is which is better - writing your own threading library or using std::thread – UKMonkey May 14 '18 at 12:23

1 Answers1

1

The only way to know is to profile. Here are some tools.

As already said in the comments, unless this is an educational exercise you probably don't want to write your own.

There is an alternative to std::list, Boost.Intrusive which may have better performance than std::list.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43