- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以前我自己曾问过这个问题,虽然不是很好。我已对此进行了改写和扩展,希望我澄清了两点。从初学者的 Angular 来看,这个问题非常多。
下面是一个简短的示例代码,该代码内联运行并作为函数的一部分运行。
代码将1加到数组的每个元素上,然后将每个元素除以2。目前这是任意的,只是占位符,可以处理更复杂但非常等效的内容。
作为背景,这会阻止实时应用程序的音频处理。这就是原因
int N = 44.1e3 * 60;
#include <iostream>
#include <stdio.h>
#include <sys/time.h>
#include <math.h>
class someClass {
int n, i; // internal class loop indeces
double foo[1000];
int I = floor(sizeof(foo)/sizeof(foo[0])); // number of elements in foo
public:
void someFunction(int);
};
void someClass::someFunction(int N){
for(n=N;n--; ){
for(i=I;i--; ){
foo[i]+= 1;
foo[i]*= .5;
}
}
}
int main(){
// this was initially an audio processing problem
// Essentially, process a minute of audio
int N = 44.1e3 * 60;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/// The Explicit Method
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int n,i; // loop indeces
double bar[1000];
int I = floor(sizeof(bar)/sizeof(bar[0]));
// START CLOCK
std::clock_t start;
double duration;
start = std::clock();
// Exactly what is defined in the function 'someFunction' above
for(n=N;n--; ){
for(i=I;i--; ){
bar[i]+= 1;
bar[i]*=.5;
}
}
// END CLOCK
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
std::cout<<"Explicit Method Time (seconds): "<< duration <<'\n';
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// The Function Method
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
someClass testClass;
// START CLOCK
start = std::clock();
testClass.someFunction(N);
// END CLOCK
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
std::cout<<"Member Function Time (seconds): "<< duration <<'\n';
return 0;
}
最佳答案
我稍微重新编写了您的代码,如下所示:
#include <iostream>
#include <chrono>
#include <cmath>
class someClass {
int n, i; // internal class loop indeces
double foo[1000];
int I = ::std::floor(sizeof(foo)/sizeof(foo[0])); // number of elements in foo
public:
void someFunction(int);
};
void someClass::someFunction(int N)
{
for(n=N; n--; ) {
for(i=I; i--; ) {
foo[i] += 1;
foo[i] *= .5;
}
}
}
int main()
{
// this was initially an audio processing problem
// Essentially, process a minute of audio
int N = 44.1e3 * 60;
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/// The Explicit Method
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int n, i; // loop indeces
double bar[1000];
int I = floor(sizeof(bar)/sizeof(bar[0]));
// START CLOCK
auto start = ::std::chrono::high_resolution_clock::now();
// Exactly what is defined in the function 'someFunction' above
for(n=N;n--; ){
for(i=I;i--; ){
bar[i]+= 1;
bar[i]*=.5;
}
}
// END CLOCK
auto end = ::std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration_secs = end - start;
std::cout<<"Explicit Method Time (seconds): "<< duration_secs.count() <<'\n';
}
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// The Function Method
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
someClass testClass;
// START CLOCK
auto start = ::std::chrono::high_resolution_clock::now();
testClass.someFunction(N);
// END CLOCK
auto end = ::std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration_secs = end - start;
std::cout<<"Member Function Time (seconds): "<< duration_secs.count() <<'\n';
}
return 0;
}
$ /tmp/a.out
Explicit Method Time (seconds): 3.89e-07
Member Function Time (seconds): 1.46e-07
::std::chrono
进行计时,并为计时您的内联版本和成员函数版本做了明确的范围。
now
的背对背调用。内联版本和成员函数之间的时间差可以完全归因于基本舍入误差。
关于c++ - 当迭代器是类成员时,为什么在C++中for循环变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47082161/
我已经在谷歌上搜索这个问题一段时间了,但我还没有找到有效的解决方案。 问题是 SSH 登录到我的服务器突然变得很慢。我可以看到身份验证需要大约 10 秒才能继续,这是我的 ssh 详细日志: Open
我正在使用 AVPlayer 在我的项目中播放在线视频。视频播放良好。现在我想减少/增加视频的 fps。以下是我正在使用的代码: self.asset = [AVAsset assetWithURL:
在 Raspberry Pi 上运行两个使用 python gpio 引脚的程序时,一个变慢。一种是磁传感器,另一种是温湿度传感器。后者是放慢速度的。它不是每 2 秒打印一次温度,而是每 5 到 10
我从 Redis 向我的应用程序提供一个 json,然后我对其进行解码和循环。 这是我从 Redis 提供的 json 的样子: [ { "titel": "test 1",
Ejabberd 版本:19.9.0 在发送 OMEMO 消息时(使用 websockets),例如
我们有相当大的代码库(150 多个项目、400000 多行 Java 代码、一些 Groovy 和 Gradle 代码、一些 Perl 代码、一些 XML、大量 JSP 等)。我设法在 Spring
我在一个网站上工作,您可以在其中创建 svg 艺术品,这意味着您可以动态添加元素、缩放、颜色并移动它们。 问题是,当你开始在他们身上施加阴影时,一切都会开始变慢。对于这个的现场演示,this是我正在开
有没有办法分析 Vim 插件? 当我打开一个大的 .py 时,我的 MacVim 变得越来越慢。我知道我可以取消选择所有插件并逐一重新选择以检查哪个插件是罪魁祸首,但是有没有更快的方法? 我的 dot
我正在构建一个JavaFX应用程序。我知道它使用反射,并且反射可能不如我在代码中构建 UI 时那么快。 所以, 如何设计我的 Controller 以使由反射引起的开销尽可能小? 带/不带 @FXML
我对 UITableViewCell 进行了子类化显示从 1 到 70 的数字。 在每个单元格中,我都在检查中奖号码并检查他们的背景。问题是,经过几次滚动后,tableview 变得非常缓慢,甚至无法
如果我想group_by 和filter 那些在数据集中有任何NA 或factor 值的,我想在 dplyr 中使用 any 函数,但发现它对 NAs 或 factor 运行缓慢(但不是为了寻找任何数
我有一个问题。在我的解决方案中,我需要将数千个数据插入数据库。我正在使用批处理准备语句在一个请求中插入多行。在我调用插入几次之后, hibernate 变得更慢了。 我猜它会在我提交后检查数据库是否有
我从 json url 获取数据,但是当我想加载图像时,速度非常慢! class NewsTableViewController: UITableViewController { var id
我有一个相当简单的托管 Realm 对象 RealmAlertItem由一些字符串和 float 组成。我有一个函数 showAlertNotification()随后被调用(从网络外部触发)并且它依
请参阅下面的表格结构。 CREATE TABLE `oarc` ( `ID` bigint(20) NOT NULL AUTO_INCREMENT, `zID` int(11) NOT N
IntelliJ 慢得像爬行。键之间没有 1-2 个延迟几乎无法打字。我已经更新了堆大小。我在我的 Macbook Pro 上运行大约 2GB RAM。自从它一直在放缓。我已经增加了堆大小,但无济于事
我的 Web 应用程序遇到了性能问题。发现瓶颈是db。应用程序在具有 4 个 CPU 和 2GB RAM 的 LAMP 服务器 (VPS) 上运行。 将新记录插入数据库(包含大约 100.000 条记
我有关于自定义 DispatchQueue 的问题。 我创建了一个队列,并将其用作captureOutput:方法的队列。这是一个代码片段: //At the file header private
我是一名移动 QA。现在我们有一个关于网络响应和 UI 渲染之间的竞争条件的问题。我们猜测如果 UI 渲染比网络响应慢,那么它就会崩溃。 我们已经尝试通过使用 Charles 的本地 map 功能来加
我在 firefox 中遇到了一些奇怪的行为,我正在构建一个单页作品集,作为一名平面设计师,编码一直很困难。我想平滑地控制导航,然后向所有元素添加缩放(最初设计为 1920x1080 全屏)。讲师扔了
我是一名优秀的程序员,十分优秀!