Can nice D code get a bit slow?

FozzieBear FozzieBear at gmail.com
Thu Mar 9 10:05:35 UTC 2023


On Wednesday, 8 March 2023 at 10:49:32 UTC, Markus wrote:
> Hi, sorry for the broad and vague question. I have read in some 
> reddit post about benchmarks, that some code didn't use the 
> final keyword on methods in a sense that final would make it 
> faster, I believe.
>
> I thought, without any D knowledge, it could be that with 
> shorter code I might create virtual calls by accident. It might 
> in some rare case have an impact on performance, but it might - 
> on the good side - be the optimizer of one of the three 
> compilers that puts that handling of maybe-virtual calls away 
> or whatever might happen.
>
> So, having no clue about D (just bought some books), I wanted 
> to ask if nice looking code can become slow, in general. In the 
> mentioned case it's just that I like the packaging of functions 
> into some sort of scope (OOP) versus the flat C and Go stuff. I 
> like OOP for this reason, but now I'm unsure whether I might 
> stay out of creating classes at all.
>
> I do like to write 'in' and 'ref' keywords at their places, for 
> maybe creating some little speed benefits and safety, but the 
> 'final' keyword I then might write because I opted in into OOP 
> looks, for just the visual reason.
>
> Uh, hope you understand my vague question, sorry about that. I 
> found D to be the right place because it's not missing any 
> essential feature I know of.
>
> Kind regards

The whole point of OO programming, made convenient by the class 
type, is to enable you to more easily work with abstractions that 
relate to your problem domain, rather than abstractions related 
to the processing of your code at the machine level (cpu, memory, 
cache, registers... etc.).

This is, in essence, the difference between C like programming, 
and OOP.

Now if 7 nanoseconds (more or less) pose a signficant performance 
issue for the solution you're developing, then OOP probably isn't 
what you're looking for anyway.

Having said that though, any info you can provide to the compiler 
so that it can (hopefully) use that information at compile time 
and insert direct calls instead of virtual calls, may be 
worthwhile. But that assumes you even know how long 7 nanoseconds 
are .. cause I don't.



More information about the Digitalmars-d-learn mailing list