public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: Fis Trivial <ybbs.daans@hotmail.com>
Cc: "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Re: Compiling c++ template is very slow.
Date: Fri, 09 Mar 2018 10:32:00 -0000	[thread overview]
Message-ID: <CAFiYyc0kfnHa-QXAsxNMB+mCfGsd51pmpM+eFDo1REJttt02sw@mail.gmail.com> (raw)
In-Reply-To: <DM5PR16MB00591703CEC97A3F2A49EAD992DE0@DM5PR16MB0059.namprd16.prod.outlook.com>

On Fri, Mar 9, 2018 at 3:33 AM, Fis Trivial <ybbs.daans@hotmail.com> wrote:
>
> I tried to use c++ template to generate code for a personal project, but
> found that the compilation time needed with g++ is much
> slower(exponentially) than with clang++.
>
> This is a code snippet for testing purpose:
>
> #include <iostream>
>
> template <int a, int b>
> struct v : v<a-1, b>, v<a, b-1>
> {
>   static int constexpr m = a;
>   static int constexpr n = b;
>   static int constexpr s = a + b;
> };
>
> template <int b>
> struct v<1, b> : v<1, b-1>
> {
>   static int constexpr m = 1;
>   static int constexpr n = b;
>   static int constexpr s = b + 1;
> };
> template <int a>
> struct v<a, 1> : v<a-1, 1>
> {
>   static int constexpr m = a;
>   static int constexpr n = 1;
>   static int constexpr s = a + 1;
> };
> template <>
> struct v<1, 1>
> {
>   static int constexpr m = 1;
>   static int constexpr n = 1;
>   static int constexpr s = 2;
> };
>
> int main()
> {
>   std::cout << v<7, 12>::s << std::endl;
>   std::cout << v<4, 3>::s << std::endl;
> };
>
>
> Here is the time information:
> ---
> $ time g++ -std=c++11 generate.cc -o bygcc
>
> real    0m39.529s
> user    0m39.418s
> sys     0m0.053s
>
> $ time clang++ -std=c++11 generate.cc -o byclang
>
> real    0m0.310s
> user    0m0.273s
> sys     0m0.024s
> ---
>
> When using greater value, gcc will require exponentially more time to
> compile while the needed time from clang grows linearly. For example,
> replacing the `main` function from above code to:
>
> int main()
> {
>   // change 7 to 8 and drop <4, 3>
>   std::cout << v<8, 12>::s << std::endl;
> };
>
> ---
> $ time g++ -std=c++11 generate.cc -o bygcc
>
> real    5m20.755s
> user    5m8.509s
> sys     0m0.260s

I can confirm this and suggest to open a bugreport.

> $ time clang++ -std=c++11 generate.cc -o byclang
>
> real    0m0.314s
> user    0m0.281s
> sys     0m0.020s
> ---
>
>
> Just for fun, making the template parameter to 128:
> ---
> $ time g++ -std=c++11 generate.cc -o bygcc
> ... not gonna happen :).
>
> $ time clang++ -std=c++11 generate.cc -o byclang
>
> real    0m18.549s
> user    0m18.410s
> sys     0m0.066s
> ---
>
>
> I am currently running Fedora 27 with following version of gcc and
> clang:
>
> $ g++ --version
> g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)
> Copyright (C) 2017 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions.  There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
> $ clang++ --version
> clang version 5.0.1 (tags/RELEASE_501/final)
> Target: x86_64-unknown-linux-gnu
> Thread model: posix
> InstalledDir: /usr/bin

  reply	other threads:[~2018-03-09 10:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-09  2:33 Fis Trivial
2018-03-09 10:32 ` Richard Biener [this message]
2018-03-09 11:51   ` Richard Biener
2018-03-09 13:08     ` Nathan Sidwell
2018-03-09 13:42       ` Richard Biener
2018-03-09 13:49         ` Nathan Sidwell

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=CAFiYyc0kfnHa-QXAsxNMB+mCfGsd51pmpM+eFDo1REJttt02sw@mail.gmail.com \
    --to=richard.guenther@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=ybbs.daans@hotmail.com \
    /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).