我需要一种快速的方法来获取64位整数中所有位的位置.例如,给定x = 123703,我想填充一个数组idx[] = {0, 1, 2, 4, 5, 8, 9, 13, 14, 15, 16}.我们可以假设我们先验地知道比特数.这将被称为10 ^ 12 - 10 ^ 15次,因此速度至关重要.到目前为止,我提出的最快答案是以下怪物,它使用64位整数的每个字节作为表的索引,这些表给出了在该字节中设置的位数以及这些位的位置:
int64_t x; // this is the input
unsigned char idx[K]; // this is the array of K bits that are set
unsigned char *dst=idx, *src;
unsigned char zero, one, two, three, four, five; // these hold the 0th-5th bytes
zero = x & 0x0000000000FFUL;
one = (x & 0x00000000FF00UL) >> 8;
two = (x & …Run Code Online (Sandbox Code Playgroud) 我有一个简单的C#和C++代码来计算点积的总和.
C#代码是:
using System;
namespace DotPerfTestCS
{
class Program
{
struct Point3D
{
public double X, Y, Z;
public Point3D(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
}
static void RunTest()
{
unchecked
{
const int numPoints = 100000;
const int numIters = 100000000;
Point3D[] pts = new Point3D[numPoints];
for (int i = 0; i < numPoints; i++) pts[i] = new Point3D(i, i + 1, i + 2);
var begin = …Run Code Online (Sandbox Code Playgroud) 我试图找到一个可靠的方法,能够准确设置我希望我的OpenGL应用程序在屏幕上呈现多少FPS.我可以通过休眠1000/fps毫秒在某种程度上做到这一点,但这没有考虑渲染所需的时间.哪种是将fps限制在所需数量的最一致方法?
我一直在研究各种游戏计时循环方法,例如Glenn Fiedler和DeWitter.由于我自己的C++知识限制,我发现关键领域难以理解.有了这个我开始尝试实现我自己的方法....我想一个好方法来尝试理解这些方法的一些东西.
[edit1:我正在使用CodeBlocks IDE和minGW-w64(x64-4.8.1-posix-seh-rev5)作为编译器]
[edit2:修改代码和输出窗口以包含第3个计时器,QueryPerformanceCounter]
在尝试完成此操作时,我遇到了以下问题:
最小代码:
#include <chrono>
#include <iostream>
#include <windows.h>
#include <stdio.h>
using namespace std;
using namespace chrono;
LARGE_INTEGER startqpc, stopqpc, li;
double PCFreq = 0.0;
void print()
{
for (int p=0; p<1000000; ) p += 1; //adjust till ms (steady) returns 1-2ms
}
int main()
{
for(int x=0; x<200; x += 1)
{
steady_clock::time_point start = steady_clock::now();
auto timePoint1 = chrono::high_resolution_clock::now();
if(!QueryPerformanceFrequency(&li))
cout << "QueryPerformanceFrequency failed!\n";
PCFreq = double(li.QuadPart)/1000.0;
QueryPerformanceCounter(&startqpc);
print();
steady_clock::time_point finish = steady_clock::now();
auto …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的立方体渲染程序中显示每秒帧数.我想看看它的表现.那么,我该怎么做呢?我已经对此进行了研究,但是我看到的例子使用了多个类但仍然无法工作,或者他们使用的是我没有的库.有没有办法通过使用像ctime这样的预安装库来获取FPS?我正在使用OpenGL和C++.
这是我的(空)函数:
void GetFPS()
{
}
Run Code Online (Sandbox Code Playgroud)
然后我在我的渲染功能中显示我的FPS:
std::cout << xRot << " " << yRot << " " << zRot << " " << FPS << "\n"; //xRot, yRot, and zRot are my cube's rotation.
Run Code Online (Sandbox Code Playgroud)
我的程序设置为60FPS,但我希望看到实际的FPS,而不是它的设置.
这是"算法",但是当我想测量执行时间时,它给我零.为什么?
#define ARRAY_SIZE 10000
...
clock_t start, end;
start = clock();
for( i = 0; i < ARRAY_SIZE; i++)
{
non_parallel[i] = vec[i] * vec[i];
}
end = clock();
printf( "Number of seconds: %f\n", (end-start)/(double)CLOCKS_PER_SEC );
Run Code Online (Sandbox Code Playgroud)
那么我该怎么做来衡量时间呢?
我需要知道如何计算C代码中函数的时间(以纳秒为单位).我试着重复这个功能直到消耗几微秒.是否还有其他功能time.h可用于计算以纳秒为单位的时间?
我一直在做一些测试,看看在循环中检查有多大区别.这是通过考虑访问数组时由C#,Java等语言插入的隐式边界检查的成本而引起的.
更新:我已经在几台额外的计算机上尝试了相同的可执行程序,这会使正在发生的事情变得更加清晰.我首先列出了原始计算机,其次是我的现代笔记本电脑.在我的现代笔记本电脑上,在循环中添加额外的检查只会增加1到4%的时间,相比之下原始硬件的3到30%.
Processor x86 Family 6 Model 30 Stepping 5 GenuineIntel ~2793 Mhz
Ratio 2 checks : 1 check = 1.0310
Ratio 3 checks : 1 check = 1.2769
Processor Intel(R) Core(TM) i7-3610QM CPU @ 2.30GHz, 2301 Mhz, 4 Core(s), 8 Logical Processor(s)
Ratio 2 checks : 1 check = 1.0090
Ratio 3 checks : 1 check = 1.0393
Processor Intel(R) Core(TM) i5-2500 CPU @ 3.30GHz, 4 Cores(s)
Ratio 2 checks : 1 check = 1.0035
Ratio 3 checks : …Run Code Online (Sandbox Code Playgroud) 我parallel_for_ 通过与简单的数组求和和乘法的正常操作进行比较,在OpenCV中进行了测试.
我有100个整数的数组,并分成10个并运行使用parallel_for_.
然后我也有正常的0到99运算进行求和和多重复用.
然后我测量了经过的时间,正常操作比parallel_for_ 操作更快.
我的CPU是Intel(R)Core(TM)i7-2600 Quard核心CPU.
parallel_for_为了求和,操作耗时0.002秒(需要2个时钟周期),乘以0.003秒(耗时3个时钟周期).
但是对于求和和乘法,正常操作需要0.0000秒(少于一个单击周期).我错过了什么?我的代码如下.
TEST类
#include <opencv2\core\internal.hpp>
#include <opencv2\core\core.hpp>
#include <tbb\tbb.h>
using namespace tbb;
using namespace cv;
template <class type>
class Parallel_clipBufferValues:public cv::ParallelLoopBody
{
private:
type *buffertoClip;
type maxSegment;
char typeOperation;//m = mul, s = summation
static double total;
public:
Parallel_clipBufferValues(){ParallelLoopBody::ParallelLoopBody();};
Parallel_clipBufferValues(type *buffertoprocess, const type max, const char op): buffertoClip(buffertoprocess), maxSegment(max), typeOperation(op){
if(typeOperation == 's')
total = 0;
else if(typeOperation == 'm')
total = 1;
}
~Parallel_clipBufferValues(){ParallelLoopBody::~ParallelLoopBody();};
virtual void …Run Code Online (Sandbox Code Playgroud) 我想知道系统最后一次启动的时间.
Environment.TickCount会起作用,但由于int的限制,它会在48-49天后破坏.
这是我一直在使用的代码:
Environment.TickCount & Int32.MaxValue
Run Code Online (Sandbox Code Playgroud)
有人知道长型返回吗?
我用它来知道系统的空闲时间:
public static int GetIdleTime()
{
return (Environment.TickCount & Int32.MaxValue)- (int)GetLastInputTime();
}
/// <summary>
/// Get the last input time from the input devices.
/// Exception:
/// If it cannot get the last input information then it throws an exception with
/// the appropriate message.
/// </summary>
/// <returns>Last input time in milliseconds.</returns>
public static uint GetLastInputTime()
{
LastInputInfo lastInPut = new LastInputInfo();
lastInPut.BlockSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
if (!GetLastInputInfo(ref lastInPut))
{
throw new Exception(GetLastError().ToString()); …Run Code Online (Sandbox Code Playgroud) c++ ×6
c ×4
performance ×4
c# ×2
frame-rate ×2
opengl ×2
time ×2
.net ×1
3d ×1
algorithm ×1
c#-4.0 ×1
c++-chrono ×1
c++11 ×1
gettickcount ×1
measurement ×1
opencv ×1
optimization ×1
windows ×1