我刚刚在内存中试验了python数据结构的大小.我写了以下片段:
import sys
lst1=[]
lst1.append(1)
lst2=[1]
print(sys.getsizeof(lst1), sys.getsizeof(lst2))
Run Code Online (Sandbox Code Playgroud)
我在以下配置上测试了代码:
52 40
所以lst1有52个字节,lst2有40个字节.48 32
48 36
任何人都可以向我解释为什么两个尺寸不同虽然两个都是包含1的列表?
在getsizeof函数的python文档中,我发现了以下内容:...adds an additional garbage collector overhead if the object is managed by the garbage collector.
在我的小例子中可能是这种情况吗?
偶然地,我发现该行char s[] = {"Hello World"};
已经正确编译,似乎被视为相同char s[] = "Hello World";
.第一个({"Hello World"}
)不是包含一个char数组的元素的数组,所以s的声明应该读取char *s[]
吗?事实上,如果我将其更改为char *s[] = {"Hello World"};
编译器,也会按预期接受它.
寻找答案,我找到的唯一提到这个的地方是这个,但没有引用标准.
所以我的问题是,char s[] = {"Hello World"};
尽管左侧是类型array of char
而右侧是类型,为什么要编译该行array of array of char
?
以下是一个工作计划:
#include<stdio.h>
int main() {
char s[] = {"Hello World"};
printf("%s", s); // Same output if line above is char s[] = "Hello World";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的任何澄清.
PS我的编译器是gcc-4.3.4.
尝试一些代码并做一些微基准测试我发现float
在包含整数的字符串上使用函数比int
在同一个字符串上使用快2倍.
>>> python -m timeit int('1')
1000000 loops, best of 3: 0.548 usec per loop
>>> python -m timeit float('1')
1000000 loops, best of 3: 0.273 usec per loop
Run Code Online (Sandbox Code Playgroud)
在测试int(float('1'))
哪个运行时比裸机短时,它变得更加奇怪int('1')
.
>>> python -m timeit int(float('1'))
1000000 loops, best of 3: 0.457 usec per loop
Run Code Online (Sandbox Code Playgroud)
我在运行cPython 2.7.6的Windows 7和使用cPython 2.7.6的Linux Mint 16下测试了代码.
我必须补充一点,只有Python 2受到影响,Python 3显示了运行时之间的差异(不显着)差异.
我知道这些微基准测试得到的信息很容易被滥用,但我很好奇为什么函数的运行时存在这样的差异.
我试图找到的实现int
和float
而不同的资料来源,我不能找到它.
当我在NSMutableDictionary
其中添加值时,自动设置Key.如何禁用它并按照第一组第一组和第二组排列.
NSMutableDictionary* filteredDictionary = [NSMutableDictionary dictionary];
[filteredDictionary setObject:@"abc" forKey:@"1"];
[filteredDictionary setObject:@"abc" forKey:@"3"];
[filteredDictionary setObject:@"abc" forKey:@"2"];
[filteredDictionary setObject:@"abc" forKey:@"5"];
[filteredDictionary setObject:@"abc" forKey:@"4"];
NSLog(@"%@",filteredDictionary);
current output:
{
1 = abc;
2 = abc;
3 = abc;
4 = abc;
5 = abc;
}
but i want
{
1 = abc;
3 = abc;
2 = abc;
5 = abc;
4 = abc;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法根据键禁用排序?
我看不出制作String.indexOf
界面的任何缺点CharSequence
.好处是其他类如StringBuffer或StringBuilder也需要实现indexOf方法.
那么有什么设计理由indexOf
应该只是其中的一部分String
吗?
谢谢.
我观察到,当使用Python3洗牌名单与random.shuffle
需要大约一半的运行时,明确地提交功能random.random
的random
关键字参数.我检查了Python2是否有同样的问题,但发现它只发生在Python3上.
我使用以下代码来测量两个版本的运行时间:
from timeit import Timer
t1 = Timer("random.shuffle(l)", "import random; l = list(range(100000))")
t2 = Timer("random.shuffle(l, random = random.random)", "import random; l = list(range(100000))")
print("With default rand: %s" % t1.repeat(10,1))
print("With custom rand: %s" % t2.repeat(10,1))
Run Code Online (Sandbox Code Playgroud)
我做了一个在ideone测试用例,为您与Python3和相同的代码中看到Python2.
根据shuffle的文档,random.random
当我省略可选关键字参数时,在默认情况下使用相同的函数random
,因此当我给它生成与默认情况下的随机数相同的函数时应该没有区别.
我检查了文件夹中shuffle
函数的相应源(Python2与Python3)Lib/random.py
,发现如果我使用random
关键字函数显式调用Python3版本,它们的行为方式相同.如果我省略这个参数,Python3使用辅助函数,_randbelow
所以应该有我的问题的根.我不明白为什么Python3会使用_randbelow
它,因为它会变慢shuffle
.据我了解,它的好处在于生成任意大的随机数,但它不应该减慢我的一个列表的速度,这个列表的次数少于2 ^ 32个元素(在我的情况下为100000).
任何人都可以向我解释为什么我在运行时看到这样的差异,虽然我使用Python3时它们应该更加接近?
PS:请注意,我不感兴趣为什么Python2的运行时比Python3好,但是rand=rand.rand
在Python3中使用参数参数而不是仅在Python3中使用它时运行时的差异.
我刚读了这篇关于未定义行为和序列点的SO C++ FAQ,并进行了一些实验.在下面的代码中gcc-4.5.2
,只在代码注释中提到的行中给出了警告,尽管之前的一行也显示了未定义的行为,不是吗?您不能说首先执行哪个加法操作数(因为+
没有序列点).为什么gcc也没有在这一行给我一个警告?
int i=0;
int j=0;
int foo(void) {
i=1;
return i;
}
int main(void) {
i = i + foo();
j = j + (j=1); //Here is a rightly warning
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮忙.
typedefs
不强类型的原因是什么?有什么好处我看不到,还是由于向后兼容?看这个例子:
typedef int Velocity;
void foo(Velocity v) {
//do anything;
}
int main() {
int i=4;
foo(i); //Should result in compile error if strongly typed.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不是要求变通方法来获得强类型数据类型,而只是想知道为什么标准不需要typedefs
强类型化?
谢谢.
public String[][] fetchData()
{
String[][] data = null;
int counter = 0;
while (counter < 10){
data[counter] = new String[] {"abc"};
counter++;
}
return data;
}
Run Code Online (Sandbox Code Playgroud)
在此循环中获取错误.请让我知道我错在哪里
我正在做一个简单的网站,我放了一些图形.现在我使用Inkscape完成每个图表.我使用Inkscape在形状之间进行UNION操作.然后我保存新图表,做另一个联合,保存,等等.我需要重复这个约150次:(
我想知道我是否可以使用javascript或D3库.或者至少是让Inkscape自动完成它的方法.
在完美的情况下,会有一个功能:
union(shapeA,shapeB): replace ShapeA and ShapeB by ShapeAunionB
Run Code Online (Sandbox Code Playgroud)
你能给我一些关于这个问题的提示吗?有帮助吗?
c ×3
python ×3
string ×3
arrays ×2
c++ ×2
java ×2
python-3.x ×2
2d ×1
charsequence ×1
diagram ×1
gcc ×1
indexof ×1
ipad ×1
iphone ×1
javascript ×1
performance ×1
python-2.7 ×1
random ×1
runtime ×1
shuffle ×1
svg ×1
typedef ×1
union ×1