public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "charles at rebelbase dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/30204]  New: std::vector operator[] 10x speedup (patch)
Date: Wed, 13 Dec 2006 18:01:00 -0000	[thread overview]
Message-ID: <bug-30204-13750@http.gcc.gnu.org/bugzilla/> (raw)

vector::operator[](size_type) in bits/stl_vector.h is currently implemented as

      reference
      operator[](size_type __n)
      { return *(begin() + __n); }

      const_reference
      operator[](size_type __n) const
      { return *(begin() + __n); }

A faster implementation would be:

      reference
      operator[](size_type __n)
      { return _M_impl._M_start[__n]; }

      const_reference
      operator[](size_type __n) const
      { return _M_impl._M_start[__n]; }

I tried a simple timing test on both implementations,
and the latter appears to be 10x faster:

(11:59:43)(charles xyzzy)(~): cat test.cc
#include <vector>
int main () {
  std::vector<int> x (100);
  unsigned long l = 0;
  const unsigned long iterations = 100000000;
  for (unsigned long i=0; i<iterations; ++i)
    l += x[50];
  return 0;
}
(12:00:14)(charles xyzzy)(~): g++ -o test test.cc -lstdc++
(12:00:22)(charles xyzzy)(~): time ./test

real    0m2.956s
user    0m2.948s
sys     0m0.008s
(12:00:27)(charles xyzzy)(~): cat test2.cc
#include <vector>
int main () {
  std::vector<int> x (100);
  unsigned long l = 0;
  const unsigned long iterations = 100000000;
  for (unsigned long i=0; i<iterations; ++i)
    l += x._M_impl._M_start[50];
  return 0;
}
(12:00:31)(charles xyzzy)(~): g++ -o test2 test2.cc -lstdc++
(12:00:39)(charles xyzzy)(~): time ./test2

real    0m0.228s
user    0m0.228s
sys     0m0.000s


-- 
           Summary: std::vector operator[] 10x speedup (patch)
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: charles at rebelbase dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30204


             reply	other threads:[~2006-12-13 18:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-13 18:01 charles at rebelbase dot com [this message]
2006-12-13 18:09 ` [Bug libstdc++/30204] " chris at bubblescope dot net
2006-12-14  2:50 ` pinskia at gcc dot gnu dot org
2006-12-14 10:34 ` pcarlini at suse dot de
2006-12-14 11:20 ` pcarlini at suse dot de
2006-12-14 17:58 ` charles at rebelbase dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-30204-13750@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).