这是一段看似非常特殊的C++代码.出于某种奇怪的原因,奇迹般地对数据进行排序使得代码几乎快了六倍.
#include <algorithm>
#include <ctime>
#include <iostream>
int main()
{
// Generate data
const unsigned arraySize = 32768;
int data[arraySize];
for (unsigned c = 0; c < arraySize; ++c)
data[c] = std::rand() % 256;
// !!! With this, the next loop runs faster.
std::sort(data, data + arraySize);
// Test
clock_t start = clock();
long long sum = 0;
for (unsigned i = 0; i < 100000; ++i)
{
// Primary loop
for (unsigned c = 0; c < arraySize; ++c) …Run Code Online (Sandbox Code Playgroud) 对于我的工作,我必须开发一个小型Java应用程序来解析非常大的XML文件(~300k行)以选择非常具体的数据(使用Pattern),所以我试图对它进行一些优化.我想知道这两个片段之间哪个更好:
if (boolean_condition && matcher.find(string)) {
...
}
Run Code Online (Sandbox Code Playgroud)
要么
if (boolean_condition) {
if (matcher.find(string)) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
更精确:
boolean_condition是boolean使用外部函数在每次迭代时计算的boolean设置为false,我不需要测试匹配的正则表达式谢谢你的帮助