关于功能模板和性能的问题 [英] Question about function templates and performance

查看:72
本文介绍了关于功能模板和性能的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我描述了我的代码并发现花了很多时间从类似结构中进行隐含的

转换。


转换主要是这样的事情


class Point

{

隐式转换为vector3; //这个转换只返回positon

Vector3的位置;

..很多东西

}

struct vector3 ///图书馆的一部分

{

浮动X,Y,Z;

}

struct lineV3 < br $>
{

Vector3 A,B;

}


类电汇

{

隐式转换为lineV3; //这个转换必须创建一个新的

结构

点A,B;

......很多其他的东西

}


假设我想要一个确定两条线之间角度的函数或

电线,

如何我是否创建了一个通用函数,它避免了

导线到lineV3的隐式转换?

此时该函数刚刚采用lineV3并且当一根导线通过时

进行转换

但这很慢,我可以复制粘贴功能,两者都有一个但是

如何让它变得通用? (没有添加太多代码)


我可以为lineV3和Wire添加一个接口,但是id必须通过属性访问

vector3,
我认为这会使它变慢或者它会得到优化

如果它只是简单地返回一个元素吗?

Colin = ^。^ =

Hi,
I profile my code and find its spending a lot of time doing implicit
conversions from similar structures.

the conversions are mainly things like this

class Point
{
implicit conversion to vector3; //this conversion just returns positon
Vector3 position;
..lots of stuff
}
struct vector3 ///part of library
{
float X,Y,Z;
}
struct lineV3
{
Vector3 A,B;
}

class Wire
{
implicit conversion to lineV3; //this conversion has to create a new
struct
Point A,B;
...lots of other stuff
}

Say I have want a function wich determines the angle between two lines or
wires,
how do I create a generic function wich avoids the implicit conversion of
wire to lineV3 ?
at the moment the function just takes lineV3 and when a wire is passed it
does the conversion
but this is slow, I could copy paste the function to have one for both but
how do I make it generic ? (without adding much code)

I could add an interface to lineV3 and Wire, but then id have to access the
vector3 via properties,
Am i right in thinking this would make it slower or would it get optimised
out if its just simply returns an element?
Colin =^.^=

推荐答案

2008年1月9日星期三09:12:24 -0800,colin< co ********* @ ntworld.NOSPAM.com>

写道:
On Wed, 09 Jan 2008 09:12:24 -0800, colin <co*********@ntworld.NOSPAM.com>
wrote:

我查看我的代码并发现它花了很多时间做隐含的
来自类似结构的
转换。
I profile my code and find its spending a lot of time doing implicit
conversions from similar structures.



真的吗?什么是很多时间?

Really? What is "a lot of time"?


[...]

说我想要一个确定两者之间角度的函数两行

或电线,

如何创建一个通用函数,避免将

wire隐式转换为lineV3?
[...]
Say I have want a function wich determines the angle between two lines
or wires,
how do I create a generic function wich avoids the implicit conversion of
wire to lineV3 ?



我怀疑数据转换会消耗很大一部分

计算该角度的时间(I'我假设它正在做通常的点积的

,除以矢量幅度,反cos的乘积。

我们在这里谈论的百分比是多少?

I''m skeptical that the data conversion would consume a significant portion
of the time it takes to calculate that angle (I''m assuming it''s doing the
usual dot-product, divide by product of vector magnitude, inverse cos).
What percentage are we really talking about here?


此时函数刚刚接受lineV3并且当一根电线通过它时

进行转换

但这很慢,我可以复制粘贴功能,两个都有一个

但是

如何让它通用? (没有添加太多代码)
at the moment the function just takes lineV3 and when a wire is passed it
does the conversion
but this is slow, I could copy paste the function to have one for both
but
how do I make it generic ? (without adding much code)



如果没有

代码的实际例子,你很难回答这个问题。现在使用。


但是,如果你能想出一种方法来代表

之间的共性,你想要共享相同代码的类,你可以把那个

的共性放到一个派生使用类的基类中,然后写一个将类型参数约束到
的泛型方法。
基类。


如果你不能这样做(例如,数字类型可能不是
实际上是相同的)然后没有一个方法检查传入数据的

类型,然后根据类型做了相应的事情,我想你是运气不好C#泛型的东西不会为算术运算符的泛型应用提供



再一次,有一个完整的例子,它会更容易提供更好的建议。

It''s difficult to answer the question without an actual example of the
code you''re using now.

But, if you can come up with a way to represent the commonality between
the classes that you want to have share the same code, you could put that
commonality into a base class from which the using classes are derived,
and then write a generic method that constrains the type parameter to the
base class.

If you can''t do this (for example, maybe the numerical types aren''t
actually the same) then short of having a single method that checks the
type of the passed in data and then does the appropriate thing according
to the type, I think you''re out of luck. The C# generic stuff doesn''t
provide for generic application of arithmetic operators.

Again, with a complete example, it''d be easier to provide better advice.


我可以为lineV3和Wire添加一个接口,但是后来id必须访问



vector3 via properties,

我认为这会让它变慢或者它会得到优惠

优化

如果它只是简单地返回一个元素?
I could add an interface to lineV3 and Wire, but then id have to access
the
vector3 via properties,
Am i right in thinking this would make it slower or would it get
optimised
out if its just simply returns an element?



如果属性足够简单,属性实际上会被内联。从您的问题中可以清楚地知道您可以使用属性来执行此操作,但如果

您可以解决属性问题,那可能是一种方式to

解决问题。


Pete

Properties do in fact get inlined if they are simple enough. It''s not
clear from your question that you could use properties to do this, but if
you can get around the issue with properties, that might be one way to
address the problem.

Pete


嗨Colin,


可以创建这样的隐式转换:


public struct Vector3 {

public double X,Y,Z;

}


公共结构点{

公共双X,Y;


public static隐式运算符Vector3(Point p){

返回new Vector3(){

X = pX,//(C#3.0语法)

Y = pY

};

}

}


不是这不是特别快,因为为每次转换创建了一个新对象

。此外,使用隐式运算符可能会非常棘手,因为它们允许您在任何可以使用的地方使用点数

Vector3s。


问候,

Bram Fokke


1月9日下午6:12,colin < colin.ro ... @ ntworld.NOSPAM.comwrote:
Hi Colin,

It''s possible to create implicit casts like this:

public struct Vector3 {
public double X, Y, Z;
}

public struct Point {
public double X, Y;

public static implicit operator Vector3(Point p) {
return new Vector3() {
X = p.X, // (C# 3.0 syntax)
Y = p.Y
};
}
}

Not that this is not particularly fast, because a new object is
created for each conversion. Furthermore, using implicit operators can
be tricky because they allow you to use Points wherever you can use
Vector3s.

Regards,
Bram Fokke

On Jan 9, 6:12 pm, "colin" <colin.ro...@ntworld.NOSPAM.comwrote:



我查看我的代码并找到它的支出很多时候从类似的结构做隐含的

转换。


转换主要是这样的事情


class点

{

隐式转换为vector3; //这个转换只返回positon

Vector3的位置;

..很多东西}


struct vector3 /// part图书馆

{

浮动X,Y,Z;}


struct lineV3

{

Vector3 A,B;


}


class Wire

{

隐式转换为lineV3; //这个转换必须创建一个新的

结构

点A,B;

......很多其他的东西


}


说我想要一个确定两条线之间角度的函数或

电线,

如何创建一个通用函数,避免将

导线隐式转换为lineV3?

此时函数刚刚接受lineV3并且当导线通过时它是否b / b
转换

但这很慢,我可以复制粘贴功能,两者都有一个但是

我该怎么做通用? (没有添加太多代码)


我可以为lineV3和Wire添加一个接口,但是id必须通过属性访问

vector3,
我认为这会使它变慢或者它会被优化

如果它只是简单地返回一个元素吗?


Colin = ^。^ =
Hi,
I profile my code and find its spending a lot of time doing implicit
conversions from similar structures.

the conversions are mainly things like this

class Point
{
implicit conversion to vector3; //this conversion just returns positon
Vector3 position;
..lots of stuff}

struct vector3 ///part of library
{
float X,Y,Z;}

struct lineV3
{
Vector3 A,B;

}

class Wire
{
implicit conversion to lineV3; //this conversion has to create a new
struct
Point A,B;
...lots of other stuff

}

Say I have want a function wich determines the angle between two lines or
wires,
how do I create a generic function wich avoids the implicit conversion of
wire to lineV3 ?
at the moment the function just takes lineV3 and when a wire is passed it
does the conversion
but this is slow, I could copy paste the function to have one for both but
how do I make it generic ? (without adding much code)

I could add an interface to lineV3 and Wire, but then id have to access the
vector3 via properties,
Am i right in thinking this would make it slower or would it get optimised
out if its just simply returns an element?

Colin =^.^=


" Peter Duniho" < Np ********* @nnowslpianmk.com在留言中写道

news:op *************** @ petes-computer.local ...
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...

2008年1月9日星期三09:12:24 -0800,colin< co ********* @ ntworld.NOSPAM .com>

写道:
On Wed, 09 Jan 2008 09:12:24 -0800, colin <co*********@ntworld.NOSPAM.com>
wrote:

>我描述了我的代码并发现它花了很多时间进行隐含的转换来自类似的结构。
>I profile my code and find its spending a lot of time doing implicit
conversions from similar structures.



真的吗?什么是很多时间?


Really? What is "a lot of time"?



隐式转换位于探查器列表的顶部!

真正的生猪就是LineIntersectLine例程。

xna Vector3.subtract操作的时间大致相同。

the implicit conversion was at the top of the list of the profiler !
the real hog is LineIntersectLine routine wich was just beneath that.
the xna Vector3.subtract operation was about the same amount of time.


>
>

> [...]
说我想要一个确定两条线之间角度的函数
或电线,如何创建一个通用函数,避免将
线隐式转换为lineV3?
>[...]
Say I have want a function wich determines the angle between two lines
or wires,
how do I create a generic function wich avoids the implicit conversion of
wire to lineV3 ?



我怀疑数据转换会消耗很大一部分

计算该角度所需的时间(I'我假设它正在做通常的点积的

,除以矢量幅度,反cos的乘积。

我们在这里谈论的百分比是多少?


I''m skeptical that the data conversion would consume a significant portion
of the time it takes to calculate that angle (I''m assuming it''s doing the
usual dot-product, divide by product of vector magnitude, inverse cos).
What percentage are we really talking about here?



数据转换在各地使用,

所以尽管与点积等相比它可能相对较快,但它的

经常调用...

实际上确切地说该函数返回角度的cos因此

反cos不使用,所以它只是一小撮浮点运算。

well the data conversion is used all over the place,
so although it may be relativly quick compared to dot product etc, its
called so often ...
actually to be exact the function returns the cos of the angle so
inverse cos isnt used, so its just a handfull of floating point ops.


>此时函数刚刚采用lineV3并且当一根导线通过时它但是这很慢,我可以复制粘贴功能,以便两者都有一个

如何让它通用? (没有添加太多代码)
>at the moment the function just takes lineV3 and when a wire is passed it
does the conversion
but this is slow, I could copy paste the function to have one for both
but
how do I make it generic ? (without adding much code)



如果没有

代码的实际例子,你很难回答这个问题。现在使用。


但是,如果你能想出一种方法来代表

之间的共性,你想要共享相同代码的类,你可以把那个

的共性放到一个派生使用类的基类中,然后写一个将类型参数约束到
的泛型方法。
基类。


如果你不能这样做(例如,数字类型可能不是
实际上是相同的)然后没有一个方法检查传入数据的

类型,然后根据类型做了相应的事情,我想你是运气不好C#泛型的东西不会为算术运算符的泛型应用提供



再一次,有一个完整的例子,它会更容易提供更好的建议。


It''s difficult to answer the question without an actual example of the
code you''re using now.

But, if you can come up with a way to represent the commonality between
the classes that you want to have share the same code, you could put that
commonality into a base class from which the using classes are derived,
and then write a generic method that constrains the type parameter to the
base class.

If you can''t do this (for example, maybe the numerical types aren''t
actually the same) then short of having a single method that checks the
type of the passed in data and then does the appropriate thing according
to the type, I think you''re out of luck. The C# generic stuff doesn''t
provide for generic application of arithmetic operators.

Again, with a complete example, it''d be easier to provide better advice.



以及一些数据项是结构而不是类,所以我不能使用基础

类不幸,

如果我使用类而不是结构,那么即时添加一大堆调用

new。

其中一个数据项是库结构。


well some of the data items are structs not classes so I cant use a base
class unfortunatly,
If I use classes instead of structs then im adding a whole load of calls to
new.
also one of the data items is a library struct.


>我可以为lineV3和Wire添加一个接口,但是后来id必须通过属性访问
vector3 ,
我是否正确地认为这会使它变得更慢或是否会优化
如果它只是简单地返回一个元素?
>I could add an interface to lineV3 and Wire, but then id have to access
the
vector3 via properties,
Am i right in thinking this would make it slower or would it get
optimised
out if its just simply returns an element?



如果属性足够简单,属性确实会内联。从您的问题中可以清楚地知道您可以使用属性来执行此操作,但如果

您可以解决属性问题,那可能是一种方式

解决问题。


Properties do in fact get inlined if they are simple enough. It''s not
clear from your question that you could use properties to do this, but if
you can get around the issue with properties, that might be one way to
address the problem.



如果是这样的话我可以试试看。因为我不能在结构中使用基类,但是我可以使用一个接口,然后我不能在接口中使用变量,所以我必须使用属性

....

另一种选择是将另一种类型存储为重复信息

但是确保它保持最新的问题也许也是如此很多。


现在我只是复制并粘贴了很多功能并更改了类型

lol ....

/ /首先只使用一个包装器并将其转换为使用单个点而不是

比行

公共静态Vector3 ClosestPointOnLineSegment(Vector3指向,LineVector3

行)

{

返回ClosestPointOnLineSegment(point,line.A,line.B);

}

public static Vector3 ClosestPointOnLineSegment(Vector3 point,Line3d line)

{

返回ClosestPointOnLineSegment(point,line.A.Position,

line.B.位置);

}

公共静态Vector3 Cl osestPointOnLineSegment(Vector3 point,Vector3 A,

Vector3 B)

{

Vector3结果;

Vector3 lineVector = B - A;

浮动长度= lineVector.Length();

Vector3 lineNormal = lineVector / length;

浮动距离= Vector3.Dot (lineNormal,point-A);

if(distance< 0)

结果= A;

其他

{

如果(距离长度)

结果= B;

其他

结果= A + lineNormal *距离;

}

返回结果;

}


//这个更小所以只需剪切和粘贴...

public static bool PointOnLineSegment(Vector3 point,LineVector3 line)

{

Vector3 pointOnLine = ClosestPointOnLineSegment(point,line);

float err = Vector3.Distance(point,pointOnLine );

profile.FunctionLeave();

if(错误Math3d.MinDistance)

返回false;

返回true;

}

public static bool PointOnLineSegment(Vector3 point,Line3d line)

{

Vector3 pointOnLine = ClosestPointOnLineSegment(point,line);

float err = Vector3.Distance(point,pointOnLine);

profile.FunctionLeave();

if(错误Math3d.MinDistance)

返回false;

返回true; < br $>
}


这样可以减少大约10秒的退出时间。

承认这只是5%左右,

但是还有很多其他类似转换的地方,

和其他很多数据类型,但我只是在点击顶部的任何东西

配置文件列表atm ...


无论如何都可以控制甚至检查功能是否内联扩展




Colin = ^。^ =

if this is so I might wel try it. as I cant use base class in a struct but I
can use an interface,
but then I cant have variables in the interface so il have to use properties
....
the other option is to store the other type as duplicate information
but the problems of making sure it is kept upto date maybe too much.

for now ive just copied and pasted lots of functions and changed the types
lol....
//first just use a wrapper and convert it to use individual points rather
than line
public static Vector3 ClosestPointOnLineSegment(Vector3 point, LineVector3
line)
{
return ClosestPointOnLineSegment(point, line.A, line.B);
}
public static Vector3 ClosestPointOnLineSegment(Vector3 point, Line3d line)
{
return ClosestPointOnLineSegment(point, line.A.Position,
line.B.Position);
}
public static Vector3 ClosestPointOnLineSegment(Vector3 point, Vector3 A,
Vector3 B)
{
Vector3 result;
Vector3 lineVector = B - A;
float length = lineVector.Length();
Vector3 lineNormal = lineVector / length;
float distance = Vector3.Dot(lineNormal, point - A);
if (distance < 0)
result = A;
else
{
if (distance length)
result=B;
else
result = A + lineNormal * distance;
}
return result;
}

// this is smaller so just cut and paste ...
public static bool PointOnLineSegment(Vector3 point, LineVector3 line)
{
Vector3 pointOnLine = ClosestPointOnLineSegment(point, line);
float err = Vector3.Distance(point, pointOnLine);
profile.FunctionLeave();
if (err Math3d.MinDistance)
return false;
return true;
}
public static bool PointOnLineSegment(Vector3 point, Line3d line)
{
Vector3 pointOnLine = ClosestPointOnLineSegment(point, line);
float err = Vector3.Distance(point, pointOnLine);
profile.FunctionLeave();
if (err Math3d.MinDistance)
return false;
return true;
}

this shaves about 10 seconds off the exectution time.
admitdely this is only about 5% percent,
but theres lots of other places where a similar conversion takes place,
and ofc lots of other data types but im just atacking anything at the top of
the profile list atm ...

is there anyway to control or even examine if functions are expanded inline
?

Colin =^.^=


这篇关于关于功能模板和性能的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆