静态变量和性能 [英] static variables and performance

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

问题描述




我有一个经常被调用的例行程序。它只是遍历一个

的日期列表并进行一些健全性检查。以下伪代码

总结了它。


无效支票()

{

const Date someDate = 1/1/2005;


(每件)

{

process(item);

}

}


上述例程经常被调用。

我的问题与构建上面的Date对象。它是

构造函数不会产生任何巨大的开销但是我很好奇

知道我是否应该让它静止。通过这样做,我很明显只需要一次调用它的构造函数,因此可以节省一些时间,但是我想知道运行时的开销是多少。使用

静态?有缺点吗?这根本不是什么大问题,我只是好奇地知道每次调用例程时是否会更好地声明为静态或

构造。


有什么意见吗?愚蠢的问题....总是*总是*意见这个

组:)

谢谢你,祝你有愉快的一天


G

Hi,

I have a a routine that is frequently called. It simply iterates over a
list of dates and does some sanity checking. The following pseudo code
summarises it.

void check()
{
const Date someDate = 1/1/2005;

for (each item)
{
process(item);
}
}

The above routine is called quite frequently.
My question relates to the construction of the Date object above. It''s
constructor doesn''t incur any great overhead however I''m curious to
know whether I should make it static. By doing that I''ll obviously
call its constructor just once and will therefore save some time at
that point, but I''m wondering about the runtime overhead of using a
static? Is there a downside? This isn''t a big issue at all, I''m just
curious to know whether it would be better declared as static or
constructed each time I invoke the routine.

Any opinions? Silly question.... there''s *always* opinions on this
group :)
thanks and have a nice day

G

推荐答案



" Gr ***** @ nospam.com" <的Gr ********** @ gmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...

"Gr*****@nospam.com" <Gr**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...


我有一个经常被调用的例行程序。它只是遍历一个日期列表并进行一些健全性检查。以下伪代码
总结了它。

void check()
{
const日期someDate = 1/1/2005;

(每个项目)
{
处理(项目);
}
}

上述例程经常被调用。
我的问题涉及上述Date对象的构造。它的构造函数并没有带来任何巨大的开销,但我很想知道我是否应该让它静止。通过这样做,我显然只需要调用它的构造函数一次,因此可以节省一些时间,但是我想知道使用
静态的运行时开销?有缺点吗?这根本不是什么大问题,我只是好奇地知道每次调用例程时是否会更好地声明为静态或
构造。
有什么意见吗?愚蠢的问题....这个
组*总是*意见:)

谢谢你,祝你有愉快的一天

G
Hi,

I have a a routine that is frequently called. It simply iterates over a
list of dates and does some sanity checking. The following pseudo code
summarises it.

void check()
{
const Date someDate = 1/1/2005;

for (each item)
{
process(item);
}
}

The above routine is called quite frequently.
My question relates to the construction of the Date object above. It''s
constructor doesn''t incur any great overhead however I''m curious to
know whether I should make it static. By doing that I''ll obviously
call its constructor just once and will therefore save some time at
that point, but I''m wondering about the runtime overhead of using a
static? Is there a downside? This isn''t a big issue at all, I''m just
curious to know whether it would be better declared as static or
constructed each time I invoke the routine.

Any opinions? Silly question.... there''s *always* opinions on this
group :)
thanks and have a nice day

G




我不是静态构造函数的专家,但我认为它根本就不存在

:-)或许我误解了你的任务,并且让我知道它是否属实。


我猜你的问题是我们是否可以构建如下的




class Date

{

public:

static Date()//构造函数

{

//一劳永逸......

}

};


上面的代码根本就没有编译。而且,我似乎不明白你的伪代码是
。为什么需要Date对象来检查其他Date对象?

Ben



I''m not an expert on static constructors but I think it simply doesn''t exist
:-) Or maybe I misunderstood your quest, and let me know if it is true.

What I guess what you are asking is if we can construct something like the
following:

class Date
{
public:
static Date() // constructor
{
// once and for all...
}
};

The above code simply doesn''t compile. And also, I don''t seem to understand
your pseudo code. Why do you need a Date object to check other Date objects?

Ben


* Ben

可能是他打算让当地的''someDate''静止......


Srini

*Ben

May be he meant to make the local ''someDate'' static...

Srini

Gr*****@nospam.com 写道:


我有一个经常被调用的例行程序。它只是遍历一个日期列表并进行一些健全性检查。以下伪代码
总结了它。

void check()
{
const日期someDate = 1/1/2005;

(每个项目)
{
处理(项目);
}
}

上述例程经常被调用。
我的问题涉及上述Date对象的构造。它的构造函数并没有带来任何巨大的开销,但我很想知道我是否应该让它静止。通过这样做,我显然只需要调用它的构造函数一次,因此可以节省一些时间,但是我想知道使用
静态的运行时开销?有缺点吗?这根本不是什么大问题,我只是好奇地知道每次调用例程时是否会更好地声明为静态或
构造。
Hi,

I have a a routine that is frequently called. It simply iterates over a
list of dates and does some sanity checking. The following pseudo code
summarises it.

void check()
{
const Date someDate = 1/1/2005;

for (each item)
{
process(item);
}
}

The above routine is called quite frequently.
My question relates to the construction of the Date object above. It''s
constructor doesn''t incur any great overhead however I''m curious to
know whether I should make it static. By doing that I''ll obviously
call its constructor just once and will therefore save some time at
that point, but I''m wondering about the runtime overhead of using a
static? Is there a downside? This isn''t a big issue at all, I''m just
curious to know whether it would be better declared as static or
constructed each time I invoke the routine.



如果它是一个常数,为什么要继续构建呢?使用静态。


Ian


If it''s a constant, why keep constructing it? Use a static.

Ian


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

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