在python 2.x中,我可以这样做:
import sys, array
a = array.array('B', range(100))
a.tofile(sys.stdout)
Run Code Online (Sandbox Code Playgroud)
然而,现在,我得到了一个TypeError: can't write bytes to text stream
.我应该使用一些秘密编码吗?
我a
坐在坐标处有一个欧几里得矢量(0, 1)
.我想旋转a
通过围绕原点90度(顺时针)(0, 0)
.
如果我对这应该如何工作有一个正确的理解,旋转后的结果(x,y)坐标应该是(1, 0)
.如果我将它旋转45度(仍然顺时针),我会期望得到的坐标(0.707, 0.707)
.
theta = deg2rad(angle);
cs = cos(theta);
sn = sin(theta);
x = x * cs - y * sn;
y = x * sn + y * cs;
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,angle
值为90.0度,结果坐标为:(-1, 1)
.而我真的很困惑.以下链接中的示例代表了上面显示的相同公式?
我做错了什么?或者我误解了矢量是如何旋转的?
我希望for循环中的iterator变量将迭代反转为0 unsigned int
,我想不出类似的比较i > -1
,就像你想做的那样signed int
.
for (unsigned int i = 10; i <= 10; --i) { ... }
Run Code Online (Sandbox Code Playgroud)
但这似乎很不清楚,因为它依赖于无符号整数的数值溢出超过10.
也许我只是没有一个清醒的头脑,但是有什么更好的方法来做到这一点......
免责声明:这只是一个简单的用例,10的上限是微不足道的,它可以是任何东西,而且i
必须是一个unsigned int
.
我做了一个简单的程序,用GCC 4.4/4.5编译如下:
int main ()
{
char u = 10;
char x = 'x';
char i = u + x;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g ++ -c -Wconversion a.cpp
我有以下内容:
a.cpp: In function ‘int main()’:
a.cpp:5:16: warning: conversion to ‘char’ from ‘int’ may alter its value
Run Code Online (Sandbox Code Playgroud)
我为以下代码得到了同样的警告:
unsigned short u = 10;
unsigned short x = 0;
unsigned short i = u + x;
a.cpp: In function ‘int main()’:
a.cpp:5:16: warning: conversion to ‘short unsigned int’ from ‘int’ may alter its …
Run Code Online (Sandbox Code Playgroud) 与任何优化问题一样,这个主题受到很多打击,但我找不到我想的东西.
很多教程,甚至SO问题都有类似的提示; 一般涵盖:
可能还有一些/很多其他人.我(出于好奇的原因)在我的应用程序中使用几个顶点缓冲区渲染2800万个三角形.我已经尝试了所有上述技术(据我所知),几乎没有性能变化.
虽然我在实施中收到大约40FPS,这绝不是问题,但我仍然很好奇这些优化'技巧'实际上在哪里使用?
我的CPU在渲染过程中空闲了大约20-50%,因此我认为我是GPU必须提高性能.
注意:我现在正在研究gDEBugger
Cross在Game Development上发布
给出以下C++代码:
struct vertex_type {
float x, y, z;
//vertex_type() {}
//vertex_type(float x, float y, float z) : x(x), y(y), z(z) {}
};
typedef struct {
vertex_type vertex[10000];
} obj_type;
obj_type cube = {
{
{-1, -1, -1},
{1, -1, -1},
{-1, 1, -1},
{1, 1, -1},
{-1, -1, 1},
{1, -1, 1},
{-1, 1, 1},
{1, 1, 1}
}
};
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我将(当前已注释掉的)构造函数添加到vertex_type
结构中时,编译时间突然增加10-15秒.难倒,我看了gcc(使用-S
)生成的程序集,发现代码大小比以前大几百倍.
...
movl $0x3f800000, cube+84(%rip)
movl $0x3f800000, cube+88(%rip)
movl $0x3f800000, …
Run Code Online (Sandbox Code Playgroud) 在分析我的反向传播算法后,我了解到它负责占用我60%的计算时间.在我开始研究并行替代方案之前,我想看看我能做些什么.
该activate(const double input[])
功能仅被占用约5%的时间.该gradient(const double input)
功能实现如下:
inline double gradient(const double input) { return (1 - (input * input)); }
Run Code Online (Sandbox Code Playgroud)
有问题的培训功能:
void train(const vector<double>& data, const vector<double>& desired, const double learn_rate, const double momentum) {
this->activate(data);
this->calculate_error(desired);
// adjust weights for layers
const auto n_layers = this->config.size();
const auto adjustment = (1 - momentum) * learn_rate;
for (size_t i = 1; i < n_layers; ++i) {
const auto& inputs = i - 1 > 0 …
Run Code Online (Sandbox Code Playgroud) 可能重复:
添加两个字符会产生int
给出以下C++代码:
unsigned char a = 200;
unsigned char b = 100;
unsigned char c = (a + b) / 2;
Run Code Online (Sandbox Code Playgroud)
逻辑上预期输出为150 ,但表达式中(a + b)
是否应该存在整数溢出?
显然必须有一个整数提升来处理这里的溢出,或者其他一些我无法看到的事情.我想知道是否有人可以启发我,所以我可以知道它是什么,我不能依赖于整数提升和溢出.
鉴于以下内容:
> '10.0.0.1'.split('.').map(parseInt)
[10, NaN, 0, 1]
Run Code Online (Sandbox Code Playgroud)
为什么输出不是:
[10, 0, 0, 1]
Run Code Online (Sandbox Code Playgroud)
尽管如下:
> x = '10.0.0.1'.split('.');
["10", "0", "0", "1"]
> x[1] == x[2]
true
Run Code Online (Sandbox Code Playgroud)
或者使用parseFloat
确实给我所需的输出; 但是我觉得我在这里缺少一些关键的东西.
编辑: '10.0.0.1'.split('.').map(function(x) { return parseInt(x); })
按预期工作.
EDIT2:我使用的是Chrome版本26.0.1410.64,但这也发生在我的node.js本地副本中.
我试图了解如何使用协同例程来"暂停"一个脚本并等待一些处理完成后再恢复.
也许我正在以错误的方式看待惯例.但我的尝试结构类似于这个答案中给出的例子.
循环loop.lua
永远不会达到第二次迭代,因此永远不会达到i == 4
退出C代码中的运行循环所需的条件.如果我不屈服loop.lua
,则此代码按预期执行.
main.cpp中
#include <lua/lua.hpp>
bool running = true;
int lua_finish(lua_State *) {
running = false;
printf("lua_finish called\n");
return 0;
}
int lua_sleep(lua_State *L) {
printf("lua_sleep called\n");
return lua_yield(L,0);
}
int main() {
lua_State* L = lua_open();
luaL_openlibs(L);
lua_register(L, "sleep", lua_sleep);
lua_register(L, "finish", lua_finish);
luaL_dofile(L, "scripts/init.lua");
lua_State* cL = lua_newthread(L);
luaL_dofile(cL, "scripts/loop.lua");
while (running) {
int status;
status = lua_resume(cL,0);
if (status == LUA_YIELD) {
printf("loop yielding\n");
} else …
Run Code Online (Sandbox Code Playgroud)