为什么TypedArrays不像通常的数组那么快?我想为CLZ使用precalc值(计算前导零函数).而且我不希望他们像往常一样解释对象?
http://jsperf.com/array-access-speed-2/2
准备代码:
Benchmark.prototype.setup = function() {
var buffer = new ArrayBuffer(0x10000);
var Uint32 = new Uint32Array(buffer);
var arr = [];
for(var i = 0; i < 0x10000; ++i) {
Uint32[i] = (Math.random() * 0x100000000) | 0;
arr[i] = Uint32[i];
}
var sum = 0;
};
Run Code Online (Sandbox Code Playgroud)
测试1:
sum = arr[(Math.random() * 0x10000) | 0];
Run Code Online (Sandbox Code Playgroud)
测试2:
sum = Uint32[(Math.random() * 0x10000) | 0];
Run Code Online (Sandbox Code Playgroud)

PS可能是我的性能测试无效随时纠正我.
在我看来,这段代码不应该编译,但似乎编译器将struct NonExistingNeverDeclaredType*参数视为前向声明(证明).但为什么?
#include <iostream>
int foo(struct NonExistingNeverDeclaredType* arg) {
return sizeof(arg);
}
int main() {
std::cout << foo(nullptr) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我的结构中有一个字段,那么为什么我的整个结构都会被打包?
例:
#include <stdio.h>
struct foo {
int a;
} __attribute__((packed));
struct bar {
char b;
struct foo bla;
char a;
};
int main() {
printf("%ld\n", sizeof(struct bar));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
barstruct的sizeof 是6,但它应该是12,因为它应该是对齐的.
为什么这段代码没有编译?如果我理解正确,这应该编译.哪里我错了?
#define THREADMODEL ASC
#if THREADMODEL==NOASC
THIS BLOCK SHOULDN'T BE COMPILED
#endif
int main() {
}
Run Code Online (Sandbox Code Playgroud) 我有一个C用Emscritpen 编译的javascript函数,它使用了一些内存(大约8mb),我需要经常调用它.(每20秒一次通话).
但是每次调用后使用的内存量都会增加.有些电话后来我得到了一个例外:
Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 100663296, (2) compile with ALLOW_MEMORY_GROWTH which adjusts the size at runtime but prevents some optimizations, or (3) set Module.TOTAL_MEMORY before the program runs.
如果我添加一些内存,它会传递更多的调用,但在再次抛出异常后.我无法理解为什么Emscripten每次尝试都需要HEAP放大,如果我每次尝试都做同样的事情.我不能在这里发布所有代码,因为它是来自大型库的功能.
PS:
在电话前的某个地方:
var render = Module.cwrap('render_djvu', 'number', ['string']);
Run Code Online (Sandbox Code Playgroud)
呼叫:
FS.createDataFile("/", "test.djvu", byteArray, true, true);
removeRunDependency();
var heapPointer = render('test.djvu');
FS.truncate('test.djvu', 0);
FS.unlink('test.djvu');
Run Code Online (Sandbox Code Playgroud)
PS:
项目代码:https://github.com/saint3k/reader,C分支.reader-js中的索引文件.使用pre.js和post.js文件在a.out.js中编译代码.要重建,请在minidjvu文件夹中使用./compile.sh.
让我们看一下这个代码示例:
#include <iostream>
int main() {
std::ios_base::sync_with_stdio(false);
int n;
std::cin >> n;
for (int i = 0; i < n; ++i) {
int buf;
std::cin >> buf;
}
}
Run Code Online (Sandbox Code Playgroud)
此代码示例在输入上的性能如下:
10000000
0
1
...
9999999
Run Code Online (Sandbox Code Playgroud)
在我的机器上:
g++-5 -O2 -std=c++11:
./a.out < input.txt 0.86s user 0.07s system 98% cpu 0.942 total
Run Code Online (Sandbox Code Playgroud)
clang-700.0.72 -O2 -std=c++11:
./a.out < input.txt 38.69s user 0.21s system 99% cpu 39.248 total
Run Code Online (Sandbox Code Playgroud)
经过一些分析后,我发现 libc++ 根本不会禁用同步。
然后我查看他们的代码,发现了这个: https: //github.com/llvm-mirror/libcxx/blob/6a85e8a355be05b9efa8408f875014e6b47cef3b/src/ios.cpp#L458
所以我的问题是,这是设计使然还是错误?
#include <iostream>
int a(int &x) {
x = -1;
return x;
}
int main () {
int x = 5;
std::cout << a(x) << " " << x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
为什么输出是"-1 5"?
PS:编译器是:
i686-apple-darwin11-llvm-g ++ - 4.2(GCC)4.2.1(基于Apple Inc. build 5658)(LLVM build 2336.11.00)
PS:没有任何优化编译.
c++ expression std expression-evaluation operator-precedence
为什么C++中的全局命名空间中有一个sort函数?为什么这段代码会编译?
#include <iostream>
#include <vector>
int main() {
std::vector<int> array(10);
sort(array.begin(), array.end());
}
Run Code Online (Sandbox Code Playgroud)
PS:clang ++ --std = c ++ 11 --stdlib = libc ++ ./test.cpp
我在远程ubuntu和本地osx上安装了两个iPython笔记本.当我尝试输出pandas数据帧时,我得到了不同的输出.查看截图.
我想在jupyter中使用ubuntu上的漂亮表格.
我该怎么办?我经常搜索,但一无所获.
让我们看一个模板函数,它接受void参数并返回void:
template<class T>
void f() {
cout << "template" << endl;
}
Run Code Online (Sandbox Code Playgroud)
后来我们专门研究这个功能:
template<>
void f<int> () {
cout << "int" << endl;
}
template<>
void f<double> () {
cout << "double" << endl;
}
int main() {
f<int> ();
f<double> ();
}
Run Code Online (Sandbox Code Playgroud)
问题是为什么这个编译?我们有三个具有相同签名的功能:
void(void),我预计它应该打破多个声明?
首先我知道t_Co,TERM = xterm-256colors等等.我不能使用macVim或gvim,因为我需要它在ssh中.但是当我在vim中设置主题(每个,除了peaksea),我得到:

我发现了一个"解决方案",它与ansi颜色相关联.当我们在这里改变它:

颜色变化.对于日晒主题,我发现一个术语主题与其他ansi颜色(接近曝光),它工作正常.但是当我需要另一个主题时,我应该为这个主题设置ansi.问题是如何在不使用标准ansi颜色的情况下设置主题?如果我们不能为什么peaksea工作正常并且不使用ANSI颜色(我尝试改变它)?
PS:iTerm一切都是一样的.
PS2:航空公司所有颜色都有效的一件有趣的事情.
当我想分配这个数组并输出第一个元素时,我得到了分段错误.我知道这个元素没有初始化,但为什么分段错误?码:
#include <iostream>
using namespace std;
int main() {
unsigned long long adj[1024][1024];
cout << adj[0][0];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在OSX上测试(1GB可用内存)和Ubuntu 12.04(大约15gb可用内存).
PS:我确信在linux中我们可以在堆栈上分配大数组.
编译器试过:
OSX(clang++, g++4.8.3 -std=g++11), Ubuntu(g++4.8.1)
Run Code Online (Sandbox Code Playgroud)
错误:
OSX:
Segmentation fault: 11
Run Code Online (Sandbox Code Playgroud)
UBUNTU:分段错误
c++ ×8
expression ×2
javascript ×2
performance ×2
c ×1
c++11 ×1
clang ×1
emscripten ×1
gcc ×1
ipython ×1
jupyter ×1
libc++ ×1
linux ×1
macos ×1
pandas ×1
stack ×1
std ×1
typed-arrays ×1
vim ×1