我目前有以下代码:
public static int currentTimeMillis()
{
long millisLong = System.currentTimeMillis();
while ( millisLong > Integer.MAX_VALUE )
{
millisLong -= Integer.MAX_VALUE;
}
return (int)millisLong;
}
Run Code Online (Sandbox Code Playgroud)
以一种int格式返回当前时间(不完全是,但它可以用于时差).出于很好的理由,我无法使用long.
是的,我只对两次通话之间的区别感兴趣,这种方法效果很好.但它看起来不错.我知道.并且效率低下.我知道.所以我的问题是,我该如何改进呢?
我需要一个返回如下的函数int:
int x1 = currentTimeMillis(); //my function
long y1 = System.currentTimeMillis();
.....
int x2 = currentTimeMillis();
long y2 = System.currentTimeMillis();
// here, x2 - x1 must be equal to y2 - y1
Run Code Online (Sandbox Code Playgroud)
编辑:
仅供参考,我想做基准测试.我正在并行运行多个线程的测试,stop事件由外部组件触发.我也是以只支持的方式序列化数据int,而我正在序列化的对象不能有long成员.
我正在尝试获取对象的第一个父级的字段和值.我目前的代码是这样的:
Class<? extends Object> cls = obj.getClass();
Field[] fields = cls.getDeclaredFields();
for ( Field field : fields )
{
String fieldName = field.getName();
String fieldValue = field.get(obj);
}
Run Code Online (Sandbox Code Playgroud)
我的类结构与此类似:
class A
{
int x;
}
class B extends A
{
int y;
}
class C extends B
{
int z;
}
Run Code Online (Sandbox Code Playgroud)
现在,我将C对象传递给方法,我希望从C和B获取所有字段,但不是从A获取.有没有办法做到这一点(使用反射,我不想实现其他方法)?
我想知道课程的大小限制是多少.我做了一个简单的测试:
#define CLS(name,other) \
class name\
{\
public: \
name() {};\
other a;\
other b;\
other c;\
other d;\
other e;\
other f;\
other g;\
other h;\
other i;\
other j;\
other k;\
};
class A{
int k;
public:
A(){};
};
CLS(B,A);
CLS(C,B);
CLS(D,C);
CLS(E,D);
CLS(F,E);
CLS(G,F);
CLS(H,G);
CLS(I,H);
CLS(J,I);
Run Code Online (Sandbox Code Playgroud)
它无法编译
"'J':上课太大了"
如果我删除最后的声明 - CLS(J,I);,这一切都很好.
这是编译器强加的限制,还是在标准的某个地方?
如果a具有包含标准和复制构造函数的类
class Ex{
//constructor definitions
}
Run Code Online (Sandbox Code Playgroud)
以及将其作为参数的函数(按值)
void F(Ex _exin){...}
Run Code Online (Sandbox Code Playgroud)
采取以下代码:
Ex A;
F(A); //F's parameter is copy constructed from A
F(Ex()); //F's parameter uses the default constructor
Run Code Online (Sandbox Code Playgroud)
在第三行中,我使用默认构造函数向F传递Ex类的新(临时)对象.我的问题是:在创建这个新对象后,它还复制构造/分配(就像它发生在第二行)或者它是直接创建"内部" F?
说我有班级:
class A
{
public:
void foo() { cout << "foo"; }
};
Run Code Online (Sandbox Code Playgroud)
并像这样调用foo:
A* a = NULL;
a->foo();
Run Code Online (Sandbox Code Playgroud)
我怀疑这会调用未定义的行为,因为它等同于(*a).foo()(或者它是?),并且取消引用a NULL 是 UB,但我找不到引用.谁能帮我吗?还是定义了?
不,功能不是virtual.不,我没有访问任何成员.
编辑:我投票结束这个问题,但不会删除它,因为我自己找不到副本,我怀疑这个标题可能更容易被其他人找到.
我已经读过C++标准允许优化到可以实际阻碍预期功能的程度.当我说这个时,我说的是返回值优化,你可能在拷贝构造函数中实际上有一些逻辑,但编译器优化了调用.
我发现这有点不好,因为在那些不知道这可能花费相当一些时间修复由此产生的错误的人.
我想知道的是,是否存在编译器过度优化可以改变功能的任何其他情况.
例如,类似于:
int x = 1;
x = 1;
x = 1;
x = 1;
Run Code Online (Sandbox Code Playgroud)
可能优化为单个x = 1;
假设我有:
class A;
A a = b;
a = b;
a = b;
Run Code Online (Sandbox Code Playgroud)
这可能也可以优化吗?可能不是最好的例子,但我希望你知道我的意思......
请考虑以下代码:
char* p = new char[2];
long* pi = (long*) p;
assert(p == pi); // OK
char* p1 = &p[1];
long* pi1 = (long*) p1;
assert(p1 == pi1); // OK
int d = p1 - p;
int d1 = pi1 - pi;
assert(d == d1); // No :(
Run Code Online (Sandbox Code Playgroud)
在此之后跑,我得d == 1和d1 == 0,虽然p1 == pi1和p == pi(我在调试器中检查这一点).这是未定义的行为吗?
我最初使用的是Visual Studio C++ Express,我已经切换到终极版,我目前很困惑为什么调试器正在移动我的断点,例如:
if(x > y) {
int z = x/y; < --- breakpoint set here
}
int h = x+y; < --- breakpoint is moved here during run time
Run Code Online (Sandbox Code Playgroud)
要么
random line of code < --- breakpoint set here
random line of code
return someValue; < --- breakpoint is moved here during run time
Run Code Online (Sandbox Code Playgroud)
它似乎在代码中的随机位置执行此操作.有时候我在这里做错了吗?我从未遇到像这样的快递版本的问题.
我减少了这个:
struct A
{
int * x;
A() : x( x = new int() )
{
}
};
Run Code Online (Sandbox Code Playgroud)
以下内容:
int m = m = 3;
//or
struct X;
//...
X x = x = X();
Run Code Online (Sandbox Code Playgroud)
对我来说似乎合法.我不明白你为什么要这样做,但它合法吗?有没有你想要这样做的int情况(不是这样,我意识到这完全没用)?
任何人都可以解释为什么由malloc()/ 管理的内存池free()被称为堆?
基于[1]:http://www.google.com/url? q = http://gee.cs.oswego.edu/dl/html/malloc.html&sa=D&sntz=1&usg=AFQjCNHaQLotbBKKwYqxiiYWN1146BWzFw"Doug Lea的解释他的malloc()如何工作",我们称之为"堆"的数据结构根本不被使用.
我们称之为"堆",因为malloc()实现使用最适合的内存块选择来返回,这在历史上是使用最小块的块来实现的,按块大小排序?
c++ ×7
java ×2
breakpoints ×1
c ×1
class ×1
constructor ×1
debugging ×1
heap ×1
malloc ×1
oop ×1
optimization ×1
reflection ×1
visual-c++ ×1