Thread overview
auto
Nov 24, 2011
RexLen
Nov 24, 2011
David Nadlinger
Nov 24, 2011
Trass3r
Nov 24, 2011
David Nadlinger
Nov 24, 2011
Trass3r
Nov 24, 2011
Tobias Brandt
Nov 24, 2011
Trass3r
Nov 24, 2011
Andrej Mitrovic
Nov 26, 2011
Peter Alexander
Nov 26, 2011
mta`chrono
November 24, 2011
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
November 24, 2011
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
November 24, 2011
> 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'.
November 24, 2011
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
November 24, 2011
>> 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?

Actually not. Would be interesting, esp. if trySemantic is in the game.
November 24, 2011
>> 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.
November 24, 2011
> The type has to be deduced anyway for type checking the
> assignment/initialization.

Makes sense. Overseen that.
November 24, 2011
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!
November 26, 2011
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.
November 26, 2011
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";
}