Old problem with performance

Christopher Wright dhasenan at gmail.com
Sun Feb 8 19:44:19 PST 2009


Weed wrote:
> Christopher Wright пишет:
>> Weed wrote:
>>> (Has started here:
>>> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=81359)
>>>
>>>
>>> To me still does not give rest performance of classes (in comparison
>>> with C++ or with D structs)
>> On my system, your struct example averaged 0.36 seconds, and your class
>> example averaged 0.38 seconds.
>>
>> Your benchmark is flawed in three ways:
>> 1. You're timing allocations in the class example. Use opAddAssign to
>> avoid it.
> 
> Earlier I proved that it is impossible. For example here in such expression:
> ==============
> space_ship_1.calculatePathTo("Moon").getCheckpoint(3).getCoords;
> 
> In this example we create a temporary class "path", create temporary
> class "checkpoint" and we take coords of checkpoint of this path. It is
> not expedient to us to store all this path and checkpoint because it is
> vary.
> ==============

In that example, you can use structs instead of classes. Your response 
to that is that structs do not participate in polymorphism.

There was a suggestion elsewhere like this:
interface IPathResolver
{
	Checkpoint getCheckpoint(Path* path, int i);
}

struct Path
{
	char[] path;
	// any other info you need....
	IPathResolver resolver;
	Checkpoint getCheckpoint(int value)
	{
		return resolver.getCheckpoint(this, value);
	}
}

This way, you only allocate once for each type you need, you have 
polymorphism, and you can put stuff on the stack for quick access.



More information about the Digitalmars-d mailing list