Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency of c++ built ins [closed]

I am fairly new to C++, having much more C experience.

I am writing a program that will use the string class, and began to wonder about the efficiency of the "length()" method.

I realized though that I didn't have a good answer to this question, and so was wondering if the answer to this and similar questions exist somewhere. While I am more than capable of determining the runtime of my own code, I'm at a bit of a loss when it comes to provided code, and so I find I can't accurately judge the efficiency of my programs.

Is there c++ documentation (online, or in "man" format) that includes information on the runtime of provided code?

Edit: I'm interested in this in general, not just string::length.

like image 759
McPherrinM Avatar asked Jul 12 '09 05:07

McPherrinM


People also ask

Is C still used in 2022?

C is one of the earliest and most widely used programming languages. C is the fourth most popular programming language in the world as of January 2022. Modern languages such as Go, Swift, Scala, and Python are not as popular as C.

Is C still viable?

The C programming language has been alive and kicking since 1972, and it still reigns as one of the fundamental building blocks of our software-studded world.

Why C is efficient and portable?

C is a portable programming language If you write a C code in your machine, it will run on any machine which supports C, without modifying a single line of code. Because it is not tied to any hardware or system. We can say, it is a hardware independent language or platform independent language.

Why C is efficient and fast?

But to answer your question, well-written C code will generally run faster than well-written code in other languages because part of writing C code "well" includes doing manual optimizations at a near-machine level.


1 Answers

At present, time complexity of size() for all STL containers is underspecified. There's an open C++ defect report for that.

The present ISO C++ standard says that STL containers should have size() of constant complexity:

21.3[lib.basic.string]/2

The class template basic_string conforms to the requirements of a Sequence, as specified in (23.1.1). Additionally, because the iterators supported by basic_string are random access iterators (24.1.5), basic_string conforms to the the requirements of a Reversible Container, as specified in (23.1).

23.1[lib.container.requirements]/5

  • Expression: a.size()
  • Complexity: (Note A)

Those entries marked ‘‘(Note A)’’ should have constant complexity

However, "should" is not a binding requirement in the Standard parlance; indeed, the above applies to std::list as well, but in practice some implementations (notably g++) have O(N) std::list::size().

The only thing that can be guaranteed is that (end() - begin()) for a string is (possibly amortized) O(1). This is because string iterators are guaranteed to be random-access, and random-access iterators are guaranteed to have constant time operator-.

As a more practical issue, for all existing C++ implementations out there, the following holds:

  • std::string::size() is O(1)
  • std::vector::size() is O(1)

They are fairly obvious, as both strings and vectors are most efficiently implemented as contiguous arrays with separately stored size: contiguous because it gives fastest element access while satisfying all other complexity requirements, and storing size is because Container requirements demand that end() be constant-time.

like image 106
Pavel Minaev Avatar answered Sep 20 '22 18:09

Pavel Minaev