为了满足高性能数学库的需求,我一直在对各种方法进行基准测试,以在 Rust vec 上进行就地操作(最好通过引用)。这些方法是:
map()
,收集到一个新的 vec,并覆盖现有的for_each()
这是基准测试代码:
use std::time::{Instant, Duration};
const N_ITEMS: usize = 100000;
const N_BENCH_TIMES: i32 = 100;
fn bench_simple() {
let mut a: Vec<i32> = vec![5; N_ITEMS];
for i in a.iter_mut() {
*i += 1;
}
}
fn bench_iterator() {
let mut a: Vec<i32> = vec![5; N_ITEMS];
let a: Vec<i32> = a.iter_mut().map(|x| *x + 1).collect();
}
fn bench_foreach() {
let mut a: Vec<i32> = vec![5; N_ITEMS];
a.iter_mut().for_each(|x| *x += …
Run Code Online (Sandbox Code Playgroud) 也许这是一个愚蠢的问题,但更快的是什么?
<?php
function getCss1 ($id = 0) {
if ($id == 1) {
return 'red';
} else if ($id == 2) {
return 'yellow';
} else if ($id == 3) {
return 'green';
} else if ($id == 4) {
return 'blue';
} else if ($id == 5) {
return 'orange';
} else {
return 'grey';
}
}
function getCss2 ($id = 0) {
$css[] = 'grey';
$css[] = 'red';
$css[] = 'yellow';
$css[] = 'green';
$css[] = 'blue';
$css[] …
Run Code Online (Sandbox Code Playgroud) obj = [1,2,3,4,5];
function iter(){
for (var key in obj){
key=key+key;
};
};
function test1() {
iter(obj);
};
function test2(){
(function iter(obj){
for (var key in obj){
key=key+key;
};
})(obj);
};
Run Code Online (Sandbox Code Playgroud)
这里,test1和test2都执行相同的操作,即使test2每次被调用时都会创建一个新函数.为什么?
快速简单的问题,我找不到一个好的解决方案:
什么是一个简单的循环,可以让处理器"咀嚼"至少十秒钟?我尝试过这样的事情,但他们眨眼间就完成了:
int max = 300000;
for (int i = 0; i < max; i++)
{
//do some random math here
}
Run Code Online (Sandbox Code Playgroud)
是否有某种计算或其他操作可以在那里占用更多时间并使用一点处理器功率?或者另一种方法来实现这个目标?
我想对几个 CUDA 内核进行一些比较分析。但是,其中一个在一个程序中运行,该程序为 GPU 加载更多工作,而另一个仅在测试工具中运行。
对于某些 GPU,这些情况意味着时钟频率会发生变化(可能不止一种时钟频率,因为有多种)。这种影响在像 Tesla T4 这样的设备(没有主动冷却)中尤为严重。
是否可以防止时钟速率因负载(或热条件)而改变?
我已经研究过这个nvidia-smi
实用程序,它有一个名为clocks
-的子命令,但它所做的只是以下内容:
clocks -- Control and query clock information.
Usage: nvidia-smi clocks [options]
options include:
[-i | --id]: Enumeration index, PCI bus ID or UUID. Provide comma
separated values for more than one device
[ | --sync-boost-list]: List all synchronous boost groups
[ | --sync-boost-add]: Add a synchronous boost group
[ | --sync-boost-remove]: Remove a synchronous boost group. Provide the group id
returned from --sync-boost-list
Run Code Online (Sandbox Code Playgroud)
......看起来这不是我需要的。当然,非 …
可能重复:
选择*vs选择列
我刚刚与我的一位同事讨论了SQL Server在存储过程中指定查询命令的性能.
所以我想知道哪一个比另一个更受欢迎,并说明背后的具体原因.
假设,我们有一个名为Employees的表
(EmpName,EmpAddress)
我们想要从表中选择所有记录.所以我们可以用两种方式编写查询,
从员工中选择*
从Employees中选择EmpName,EmpAddress
所以我想知道上述查询中是否存在任何特定的差异或性能问题,或者它们是否与SQL Server引擎相同.
更新:
我一直以为c数组会比C++中的std :: array快,但是我根据访问速度做了一些基准测试,看起来std :: array更快.这是为什么?
我想对for循环进行基准测试.我决定将for循环中的变量增加100并相应地测量时间.
#include <cstdio>
#include <ctime>
#include <time.h>
#include <iostream>
#include <random>
#include <iomanip> // std::setprecision
using namespace std;
double difference(timespec start, timespec end);
int main()
{
timespec time1, time2;
for(int limit = 0; x < 100000; limit+= 100)
{
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
int temp = 0;
for (int i = 0; i< limit; i++)
temp+=temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
std::cout << std::fixed;
std::cout << std::setprecision(5);
cout<<x <<" " << difference(time1,time2)<<endl;
}
return 0;
}
double difference(timespec start, timespec end)
{
timespec temp;
if …
Run Code Online (Sandbox Code Playgroud) 我有两个相同的脚本.在一个我使用的猫和其他它都是AWK.
这是第一个:
#!/bin/bash
lines=$(cat /etc/passwd | wc -l)
for ((i=1 ; i <=$lines ; i++ ))
do
user=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $1}')
uid=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $3}')
gid=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $4}')
shell=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $7}')
echo -e "User is : $user \t Uid is : $uid \t Gid is : $gid \t Shell is : $shell"
done
Run Code Online (Sandbox Code Playgroud)
这是第二个: …
这里对Golang来说很新,并且在使用goroutines进行基准测试时遇到了问题.
我的代码在这里:
type store struct{}
func (n *store) WriteSpan(span interface{}) error {
return nil
}
func smallTest(times int, b *testing.B) {
writer := store{}
var wg sync.WaitGroup
numGoroutines := times
wg.Add(numGoroutines)
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < numGoroutines; n++ {
go func() {
writer.WriteSpan(nil)
wg.Done()
}()
}
wg.Wait()
}
func BenchmarkTest1(b *testing.B) {
smallTest(1000000, b)
}
func BenchmarkTest2(b *testing.B) {
smallTest(10000000, b)
}
Run Code Online (Sandbox Code Playgroud)
它看起来两个场景的运行时和分配应该是相似的,但运行它们会给我以下结果,这些结果有很大的不同.不知道为什么会这样?那些额外的分配来自哪里?
BenchmarkTest1-12 1000000000 0.26 ns/op 0 B/op 0 allocs/op
BenchmarkTest2-12 1 2868129398 ns/op 31872 B/op …
benchmarking ×10
performance ×3
c++ ×2
time ×2
arrays ×1
awk ×1
bash ×1
c# ×1
cat ×1
clock ×1
cuda ×1
for-loop ×1
go ×1
goroutine ×1
javascript ×1
optimization ×1
php ×1
profiling ×1
rust ×1
select ×1
sql ×1
sql-server ×1
vector ×1