www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - auto

reply RexLen <rexlen gmail.com> writes:
Just curious:
is there any performance gap using auto instead of int or other
type? For example

int[] ar1 = new int[1000];
auto[] ar2 = new int[1000];

are these equivalent by a perfomance point of view?

Thx
Nov 24 2011
next sibling parent David Nadlinger <see klickverbot.at> writes:
On 11/24/11 9:59 PM, RexLen wrote:
 Just curious:
 is there any performance gap using auto instead of int or other
 type? For example

 int[] ar1 = new int[1000];
 auto[] ar2 = new int[1000];

 are these equivalent by a perfomance point of view?
The actual type »auto« represents is determined during compilation, so there is no difference in the final executable. If you wanted to actually use a type not known statically, I'd have a look at std.variant. Hope this helps, David
Nov 24 2011
prev sibling next sibling parent reply Trass3r <un known.com> writes:
 is there any performance gap using auto instead of int or other
 type? For example

 int[] ar1 = new int[1000];
 auto[] ar2 = new int[1000];

 are these equivalent by a perfomance point of view?
Well the runtime performance is equal but of course compilation takes slightly longer since it has to deduce the type first. But in general you should only use auto if it doesn't make your code harder to understand (e.g. auto x = funcThatDoesCrazyShitYouCantGuessFromTheName(); // so, what's the type of x???). Also it's 'auto ar2' instead of 'auto[] ar2'.
Nov 24 2011
next sibling parent reply David Nadlinger <see klickverbot.at> writes:
On 11/24/11 10:09 PM, Trass3r wrote:
 Well the runtime performance is equal but of course compilation takes
 slightly longer since it has to deduce the type first.
Just curious: I would be surprised if there was actually a measurable difference between the two – did you ever try to measure it? David
Nov 24 2011
parent Trass3r <un known.com> writes:
 Well the runtime performance is equal but of course compilation takes=
 slightly longer since it has to deduce the type first.
Just curious: I would be surprised if there was actually a measurable =
=
 difference between the two =E2=80=93 did you ever try to measure it?
Actually not. Would be interesting, esp. if trySemantic is in the game.
Nov 24 2011
prev sibling parent reply Tobias Brandt <tob.brandt googlemail.com> writes:
 is there any performance gap using auto instead of int or other>> type? For
example>> >> int[] ar1 = new int[1000];>> auto[] ar2 = new int[1000];>> >> are
these equivalent by a perfomance point of view?
 Well the runtime performance is equal but of course compilation takes
slightly longer since it has to deduce the type first.
The type has to be deduced anyway for type checking the assignment/initialization.
Nov 24 2011
parent reply Trass3r <un known.com> writes:
 The type has to be deduced anyway for type checking the
 assignment/initialization.
Makes sense. Overseen that.
Nov 24 2011
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
But I bet you would waste more memory at compile-time if you had to
type a long template instance name instead of using auto. We're
talkin' bytes here!
Nov 24 2011
parent Peter Alexander <peter.alexander.au gmail.com> writes:
On 24/11/11 10:38 PM, Andrej Mitrovic wrote:
 But I bet you would waste more memory at compile-time if you had to
 type a long template instance name instead of using auto. We're
 talkin' bytes here!
Actually, using `auto` should be faster because the compiler doesn't need to do any name lookup. auto x = foo(); // deduce type of Foo and use that Foo x = foo(); // find out what 'Foo' refers to, but also deduce type of foo() and then do type checking.
Nov 26 2011
prev sibling parent mta`chrono <chrono mta-international.net> writes:
Did you know that auto is not auto in the way auto behalfs. it's just a
placeholder so the compiler can determine that a variable follows. but
you can use any other keyword for type interfering, too.

void main()
{
    const a = FunctionThatReturnsSomething();
    static b = 5.0;
    scope c = "hey";
}
Nov 26 2011