我正在比较两种方法来过滤列表,使用和不使用流.事实证明,对于10,000个项目的列表,不使用流的方法更快.我有兴趣理解为什么会这样.有人能解释一下结果吗?
public static int countLongWordsWithoutUsingStreams(
final List<String> words, final int longWordMinLength) {
words.removeIf(word -> word.length() <= longWordMinLength);
return words.size();
}
public static int countLongWordsUsingStreams(final List<String> words, final int longWordMinLength) {
return (int) words.stream().filter(w -> w.length() > longWordMinLength).count();
}
Run Code Online (Sandbox Code Playgroud)
使用JMH的Microbenchmark:
@Benchmark
@BenchmarkMode(Throughput)
@OutputTimeUnit(MILLISECONDS)
public void benchmarkCountLongWordsWithoutUsingStreams() {
countLongWordsWithoutUsingStreams(nCopies(10000, "IAmALongWord"), 3);
}
@Benchmark
@BenchmarkMode(Throughput)
@OutputTimeUnit(MILLISECONDS)
public void benchmarkCountLongWordsUsingStreams() {
countLongWordsUsingStreams(nCopies(10000, "IAmALongWord"), 3);
}
public static void main(String[] args) throws RunnerException {
final Options opts = new OptionsBuilder()
.include(PracticeQuestionsCh8Benchmark.class.getSimpleName())
.warmupIterations(5).measurementIterations(5).forks(1).build();
new Runner(opts).run();
} …Run Code Online (Sandbox Code Playgroud) 我正在做一个Maven项目。情况如下所示...
class Test {
public void applyAll() {
....................
....................
Collection<Migratable> applicableUpdates = SomeOtherClass.getApplicableUpdates();
doUpdate(applicableUpdates);
}
@Benchmark
public void apply(Migratable m) {
....................
....................
}
private void doUpdate(Collection<Migratable> applicableUpdates) throws Exception {
for (Migratable m : applicableUpdates) {
try {
apply(m);
} catch (Exception e) {
logger.error("Falid to apply migration {}" + m, e);
throw e;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要计算执行每次迁移需要多长时间。我只需要计算apply(Migratable m)方法的执行时间。
现在,当我使用“ mvn clean install”构建项目时,构建失败,并且显示“方法参数应为@State类”。
在这里,参数来自另一个方法doUpdate(Collection applyUpdates) [请参见场景]。那么如何在给定的情况下摆脱这个错误呢?
当人们试图在各种库中执行严格的基准测试时,我有时会看到这样的代码:
auto std_start = std::chrono::steady_clock::now();
for (int i = 0; i < 10000; ++i)
for (int j = 0; j < 10000; ++j)
volatile const auto __attribute__((unused)) c = std_set.count(i + j);
auto std_stop = std::chrono::steady_clock::now();
Run Code Online (Sandbox Code Playgroud)
将volatile在这里用来防止优化注意到,被测代码的结果被丢弃,并且然后丢弃整个计算.
当测试中的代码没有返回值时,说它是void do_something(int),那么有时候我会看到这样的代码:
auto std_start = std::chrono::steady_clock::now();
for (int i = 0; i < 10000; ++i)
for (int j = 0; j < 10000; ++j)
static_cast<volatile void> (do_something(i + j));
auto std_stop = std::chrono::steady_clock::now();
Run Code Online (Sandbox Code Playgroud)
这是正确用法volatile吗?什么是volatile void?从编译器和标准的角度来看,它意味着什么? …
我愿意编写一个代码,让我的CPU执行一些操作,看看他花了多少时间来解决它们.我想做一个从i = 0到i <5000的循环,然后将i乘以一个常数和时间.我最终得到了这个代码,它没有错误,但即使我更改循环i <49058349083或者如果i <2它需要相同的时间,它只需要0.024秒来执行代码.是什么错误?
PD:我昨天开始学习C++我很抱歉,如果这是一个非常容易回答的问题,但我找不到解决方案
#include <iostream>
#include <ctime>
using namespace std;
int main () {
int start_s=clock();
int i;
for(i=0;i<5000;i++){
i*434243;
}
int stop_s=clock();
cout << "time: "<< (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 注意:问题末尾给出了详尽的系统详细信息。
我试图让我的开发机器具有非常稳定的 CPU 频率,以便我可以获得一些线性代数代码的精确基准 - 然而,它仍然显示出明显的频率波动。
我已将缩放调节器设置为performance模式:
echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
Run Code Online (Sandbox Code Playgroud)
我还锁定了C0状态中的所有内容:
sudo cpupower idle-set -D 0
Run Code Online (Sandbox Code Playgroud)
然而,使用 时i7z,我仍然观察到 CPU 频率波动 >5%。我非常感谢有关我是否应该采取进一步步骤将 CPU 频率限制为固定值,或者是否无法获得更精确的频率限制的指导。
附录:
我的处理器是 Intel Core i7-9750H:
cat /proc/cpuinfo | grep 'model name'
model name : Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
...
Run Code Online (Sandbox Code Playgroud)
它有 12 个逻辑 CPU 和 6 个物理核心:
lscpu -a -e
CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE MAXMHZ MINMHZ
0 0 0 0 0:0:0:0 yes 4500.0000 800.0000
1 …Run Code Online (Sandbox Code Playgroud) gcc/g++ 是否具有启用或禁用算术优化的标志,例如 wherea+a+...+a被n*awhen替换a为整数?特别是,在使用-O2or时可以禁用它-O3吗?
在下面的示例中,即使-O0将加法运算替换为单个乘法:
$ cat add1.cpp
unsigned int multiply_by_22(unsigned int a)
{
return a+a+a+a+a+a+a+a+a+a+a+a+a+a+a+a+a+a+a+a+a+a;
}
$ g++ -S -masm=intel -O0 add1.cpp
$ cat add1.s
...
imul eax, edi, 22
Run Code Online (Sandbox Code Playgroud)
即使禁用-O0(参见g++ -c -Q -O0 --help=optimizers | grep enabled)中使用的所有标志仍然会产生imul操作。
添加循环时,需要-O1将重复加法简化为单个乘法:
$ cat add2.cpp
unsigned int multiply(unsigned int a, unsigned int b)
{
unsigned int sum=0;
for(unsigned int i=0; i<b; i++)
sum …Run Code Online (Sandbox Code Playgroud) 我想评估使用不同文件类型(geotiff、二进制)或对象(RasterBrick、RasterStack)从栅格时间序列中提取数据的时间。我创建了一个函数,该函数将从栅格对象的随机点提取时间序列,然后使用微基准测试它。
前任。:
# read a random point from a raster stack
sample_raster <- function(stack) {
poi <- sample(ncell(stack), 1)
raster::extract(stack, poi)
}
# opening the data using different methods
data_stack <- stack(list.files(pattern = '3B.*tif'))
data_brick <- brick('gpm_multiband.tif')
bench <- microbenchmark(
sample_stack = sample_raster(data_stack),
sample_brick = sample_raster(data_brick),
times = 10
)
boxplot(bench)
# this fails because sampled point is different
bench <- microbenchmark(
sample_stack = sample_raster(data_stack),
sample_brick = sample_raster(data_brick),
times = 10,
check = 'equal'
)
Run Code Online (Sandbox Code Playgroud)
我在此处包含了我的数据集的示例
由此,我可以看到RasterBrick上的采样比堆栈更快(R …
我有一个类似的代码:
...
void benchMark(benchmark::State& state){
maxCapacity = state.range(0);
// set up some stuff
for (auto _ : state){
// time this code
}
}
BENCHMARK(benchMark)->DenseRange(2, 10, 1);
BENCHMARK_MAIN();
Run Code Online (Sandbox Code Playgroud)
我想把它改成这样:
...
void benchMark(benchmark::State& state){
maxCapacity = state.range(0);
// set up some stuff
for (auto _ : state){
// time this code
}
}
int main(){
BENCHMARK(benchMark)->DenseRange(2, 10, 1);
}
Run Code Online (Sandbox Code Playgroud)
我这样做只是为了以后可以为代码提供命令行参数支持。现在这段代码编译成功,但我根本没有得到任何输出。我猜代码甚至没有运行。第一个代码需要 5 分钟才能完成,但第二个代码几乎立即完成执行。我究竟做错了什么?
任何帮助都会很棒。谢谢..
编辑:
由于我无法分享完整的代码,这里有一个最小的可重现示例:
#include <iostream>
#include <benchmark/benchmark.h>
using namespace std;
void pointQuery(int maxCapacity){
long sum = 0;
for(int i=0; …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的程序来测量一个函数花费了多少时间。
#include <iostream>
#include <vector>
#include <chrono>
struct Foo
{
void addSample(uint64_t s)
{
}
};
void test(const std::vector<uint64_t>& samples)
{
uint32_t onlyCallTime = 0;
uint32_t loopOnlyTime = 0;
Foo stats;
std::chrono::high_resolution_clock::time_point callStart,callEnd;
auto start = callStart = callEnd = std::chrono::high_resolution_clock::now();
for(auto &s : samples)
{
callStart = std::chrono::high_resolution_clock::now();
loopOnlyTime += std::chrono::duration_cast<std::chrono::microseconds>(callStart-callEnd).count();
stats.addSample(s);
callEnd = std::chrono::high_resolution_clock::now();
onlyCallTime += std::chrono::duration_cast<std::chrono::microseconds>(callEnd-callStart).count();
}
auto end = std::chrono::high_resolution_clock::now();
std::cout << "overall duration: " << std::chrono::duration_cast<std::chrono::microseconds>(end-start).count() << std::endl;
std::cout << "only call duration: " << …Run Code Online (Sandbox Code Playgroud) microbenchmark ×10
benchmarking ×5
c++ ×4
jmh ×2
x86 ×2
c ×1
c++-chrono ×1
cpu ×1
cpuid ×1
duration ×1
gcc ×1
intel ×1
java ×1
java-8 ×1
java-stream ×1
linux ×1
list ×1
performance ×1
r ×1
r-raster ×1
rdtsc ×1
void ×1
volatile ×1