下面是一个简单的过程编码C#和Python(对于你们好奇的过程,它是项目欧拉问题5的解决方案).
我的问题是,C#下面的代码只需要9秒来迭代,而Python代码的完成需要283秒(确切地说,Python 3.4.3上的283秒 - 64位和Python 2.7.9上的329秒--32位).
到目前为止,我已经编写了类似的进程C#,Python并且执行时间差异可比.然而,这一次,经过的时间之间存在极大的差异.
我认为,这种差异的某些部分来自灵活变量类型的python语言(我怀疑,python将变量的某些部分转换为double)但是这仍然很难解释.
我究竟做错了什么?
我的系统:Windows-7 64位,
C# - VS Express 2012(9秒)
Python 3.4.3 64位(283秒)
Python 2.7.9 32位(329秒)
c-sharp代码:
using System;
namespace bug_vcs {
class Program {
public static void Main(string[] args) {
DateTime t0 = DateTime.Now;
int maxNumber = 20;
bool found = false;
long start = maxNumber;
while (!found) {
found = true;
int i = 2;
while ((i < …Run Code Online (Sandbox Code Playgroud) 对于下面的代码: (1) "main"调用函数"f1". (2)函数"f1"做一些数字运算; 使用malloc创建一个"char"数组,然后将数组的指针返回给main(不分配-freeing-数组).
我有3个与案例相关的问题: (1)我假设,虽然函数"f1"已经终止,但分配的char数组仍然保持分配状态,直到主程序完全终止.也就是说,分配的内存仍然属于主内存,没有其他进程可以从外部访问(我的意思是,干扰)它.我对吗? (2)在程序终止之前是否必须释放数组(在"f1"中分配)(或者在主程序终止后它是否被释放)? (3)如果第二个问题的答案是"是",那么如何释放在另一个函数中分配的数组?
注意:我希望保持在纯c的范围内,而不是溢出到c ++.
char *f1 (...) {
...
...
char *fTmp = malloc (length1 * sizeof (char));
char *fData = malloc (length2 * sizeof (char));
...
...
free (fTmp);
return (fData);
}
int main () {
char *fData = f1 (...);
...
return (0);
}
Run Code Online (Sandbox Code Playgroud) 下面的行(纯c)在windows(win7 64位+代码块13 + mingw32)和debian(wheezy 32位+代码块10 + gcc)上干净地编译,但在kali(64位+代码块+ gcc)上引发警告.任何意见?我的意思是,为什么我会收到这个警告,虽然同一行编译没有窗口和debian上的任何警告?
void* foo(void *dst, ...) {
// some code
unsigned int blkLen = sizeof(int); // this line ok.
unsigned int offset = (unsigned int) dst % blkLen; // warning here!
// some code cont...
}
Run Code Online (Sandbox Code Playgroud)
codeblocks中的消息是:" error:从指针转换为不同大小的整数[-Werror = pointer-to-int-cast]"
注意:我的编译器选项是-std=c99 -Werror -save-temps(在所有三个系统上都相同).
编辑2:虽然我已经设法使用下面的预处理器行编译无警告,但@Keith Thompson(见下文)对此问题有一个关键点.所以,我最后的决定是使用uintptr_t将是一个更好的选择.
编辑1:谢谢大家的回复.正如所有回复所述,问题是32位与64位问题.我插入了以下预处理行:
#if __linux__ // or #if __GNUC__
#if __x86_64__ || __ppc64__
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#else …Run Code Online (Sandbox Code Playgroud) 有没有办法(在纯C中)区分malloced字符串和字符串文字,而不知道哪个是哪个?严格地说,我正试图找到一种方法来检查变量是否是一个malloced字符串,如果是,我会释放它; 如果没有,我会放手.
当然,我可以向后挖掘代码并确保变量是否被malloc编辑,但以防万一存在简单方法...
编辑:添加行以使问题更具体.
char *s1 = "1234567890"; // string literal
char *s2 = strdup("1234567890"); // malloced string
char *s3;
...
if (someVar > someVal) {
s3 = s1;
} else {
s3 = s2;
}
// ...
// after many, many lines of code an algorithmic branches...
// now I lost track of s3: is it assigned to s1 or s2?
// if it was assigned to s2, it needs to be freed;
// if …Run Code Online (Sandbox Code Playgroud) 在 python 中,是否有命令(或指令)在为变量分配与先前分配的类型不同的值时引发警告?
x = int() # "x" declared as integer
y = float() # "y" declared as float
x = 5 # "x" assigned an integer
y = 2.75 # "y" assigned a float
print(x) # prints "5"
print(y) # prints "2.75"
x = y # !!! "x" is assigned a float; no warning raised !!!
print(x) # prints 2.75
Run Code Online (Sandbox Code Playgroud) 我是Assembly的新手,下面的代码应该通过两个不同的函数交换两个整数:首先使用swap_c然后再使用swap_asm.
但是,我怀疑,我是否需要push(我的意思是保存)汇编代码之前的每个寄存器值以及pop它们之后(就在返回之前main).换句话说,如果我在运行函数后返回不同的寄存器内容(不是像ebp或者esp只是eax,但是,只是ebx,ecx&edx),CPU会不会生我的气swap_asm?取消组装部件中的线条是否更好?
这段代码对我来说运行正常,我设法将27行汇编C代码减少到7个装配线.
ps:系统是Windows 10,VS-2013 Express.
main.c 部分
#include <stdio.h>
extern void swap_asm(int *x, int *y);
void swap_c(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int main(int argc, char *argv[]) {
int x = 3, y = 5;
printf("before swap => x = %d y = …Run Code Online (Sandbox Code Playgroud) 以下max函数应该返回5,但它返回4294967294.我怀疑这种奇怪的行为是由于铸造变量引起的,但却无法弄清楚.有人可以检测到故障吗?
系统:Windows 7(64位),mingw64
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdarg.h>
#include <inttypes.h>
int64_t max(int64_t n, ...) {
va_list v;
int64_t i, t, max = INT64_MIN;
va_start(v, n);
for (i = 0; i < n; i++) {
t = va_arg(v, int64_t);
if (max < t) {
max = t;
}
}
va_end(v);
return (max);
}
int main(int argc, char **argv) {
printf("max(3, 1, 5, -2) : %3I64d\n", max(3, 1, 5, -2));
return (0);
}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
int main() {
char *buffer = "hello";
char *words = malloc(6 * sizeof(char));
int count = 0;
while (count < 10) {
*(words+count) = buffer[count];
count++;
}
printf("%s\n", words);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道乘以一个数字有什么区别sizeof(char)。
例如,如果我删除 6 (just char *words=malloc(sizeof(char));),代码也可以工作。
我认为它不会起作用,但是当我运行它时它会起作用。
至于试图连接两个字符串(字符数组),下面的代码通过返回正确的结果return,但不是通过引用传递的参数(即,预期结果char *dst的StrAdd功能).
关于"后"的结果; printf为st打印正确的连接字符串.但变量"s1"应该包含连接的字符串.然而,s1打印出一些奇怪的东西.
有人能弄清楚它有什么问题吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *StrAdd (char *dst, const char *src) {
/* add source string "src" at the end of destination string "dst"
i.e. concatenate
*/
size_t lenDst = strlen (dst);
size_t lenSrc = strlen (src);
size_t lenTot = lenDst + lenSrc + 1;
char *sTmp = (char *) malloc (lenTot);
memcpy (&sTmp [0], &dst [0], lenDst);
memcpy (&sTmp [lenDst], &src [0], lenSrc);
sTmp [lenTot …Run Code Online (Sandbox Code Playgroud) 例如,有 5 个(或更多)任务(方法)返回true或false;如果任何连续的方法返回false,我正在使用算法语法来打破链,否则继续向内。目前,我已经构建了一堆 if 块,它们相互链接;然而,对我来说似乎很难看,并且很难通过一堆论点阅读。
if (Method_1_Returns(String manyArgs, ...) == true) {
if (Method_2_Returns(String manyArgs, ...) == true) {
if (Method_3_Returns(String manyArgs, ...) == true) {
if (Method_4_Returns(String manyArgs, ...) == true) {
...
}
else {
MethodToShowError(someString_4);
}
}
else {
MethodToShowError(someString_3);
}
}
else {
MethodToShowError(someString_2);
}
}
else {
MethodToShowError(someString_1);
}
Run Code Online (Sandbox Code Playgroud)
我打算将其转换为一个switch块,但除了讨厌该goto指令外,这似乎不符合编码标准(或者,是吗?)。
String temp = "1";
switch (temp) {
case "1":
if (Method_1_Returns(String manyArgs, ...)) goto case …Run Code Online (Sandbox Code Playgroud)