小编Bal*_*ala的帖子

当模块名称中包含" - "破折号或连字符时,如何导入模块?

我想导入foo-bar.py.这有效:

foobar = __import__("foo-bar")
Run Code Online (Sandbox Code Playgroud)

这不是:

from "foo-bar" import *
Run Code Online (Sandbox Code Playgroud)

我的问题:有什么方法可以使用上面的格式,即from "foo-bar" import *导入一个包含其中的模块-

python import module hyphen

160
推荐指数
5
解决办法
7万
查看次数

一个循环中的两个操作与两个循环在每个循环中执行相同的操作

这个问题与这 两个循环体或一个(结果相同)相同, 但在我的情况下,我使用Java.

我有两个循环,运行十亿次.

int a = 188, b = 144, aMax = 0, bMax = 0;

for (int i = 0; i < 1000000000; i++) {
  int t = a ^ i;
  if (t > aMax) 
    aMax = t;     
}  

for (int i = 0; i < 1000000000; i++) {
  int t = b ^ i;
  if (t > bMax) 
    bMax = t;     
}  
Run Code Online (Sandbox Code Playgroud)

在我的机器中运行这两个循环所需的时间是4秒.当我将这两个循环融合到一个循环中并在该单循环中执行所有操作时,它将在2秒内运行.正如您所看到的那样,琐碎的操作构成了循环内容,因此需要恒定的时间.

我的问题是我在哪里获得这种性能提升?

我猜测性能在两个独立的循环中受影响的唯一可能的地方是它增加i并检查我是否<1000000000 20亿次而不是10亿次如果我将循环融合在一起.还有其他事吗?

谢谢!

java performance loops

10
推荐指数
1
解决办法
6737
查看次数

在String.format()中结合使用String和Varargs

想知道是否可以在String.format()中组合单个字符串和varargs字符串,如下所示:

String strFormat(String template, String str, String... moreStrs) {    
    return String.format(template, str, moreStrs);
}
Run Code Online (Sandbox Code Playgroud)

如果我这样打电话:

strFormat("%s/%s/%s", "hello", "world", "goodbye");
Run Code Online (Sandbox Code Playgroud)

我得到java.util.MissingFormatArgumentException:格式说明符's'

这有效:

String strFormat(String template, String... moreStrs) {    
    return String.format(template, moreStrs);
}
Run Code Online (Sandbox Code Playgroud)

除此之外:

String strFormat(String template, String str1, String str2) {    
    return String.format(template, str1, str2);
}
Run Code Online (Sandbox Code Playgroud)

有可能让这个工作吗?

String strFormat(String template, String str, String... moreStrs) {    
    return String.format(template, str, moreStrs);
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

java string.format variadic-functions

5
推荐指数
1
解决办法
2988
查看次数

不包括stdlib.h不会产生任何编译错误!

希望这是一个非常简单的问题.以下是我的C pgm(test.c).

#include <stdio.h>
//#include <stdlib.h>

int main (int argc, char *argv[]) {
    int intValue = atoi("1");
    double doubleValue = atof("2");
    fprintf(stdout,"The intValue is %d and the doubleValue is %g\n", intValue, doubleValue);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

请注意,我正在使用stdlib.h中的atoi()和atof(),但我没有包含该头文件.我编译pgm(gcc test.c)并且没有编译错误!

我运行pgm(./a.out),这是输出,这是错误的.

The intValue is 1 and the doubleValue is 0
Run Code Online (Sandbox Code Playgroud)

现在我包含stdlib.h(通过删除#include之前的注释)并重新编译它并再次运行它.这次我得到了正确的输出:

The intValue is 1 and the doubleValue is 2
Run Code Online (Sandbox Code Playgroud)

为什么编译器没有抱怨不包含stdlib.h并且仍然让我使用atoi(),atof()函数?

我的gcc信息:

$ gcc --version
gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-27)
Run Code Online (Sandbox Code Playgroud)

任何想法赞赏!

c gcc std atof

4
推荐指数
1
解决办法
5378
查看次数

如何在bash shell脚本的变量中容纳空格?

希望这应该是一个简单的...这是我的test.sh文件:

#!/bin/bash
patch_file="/home/my dir/vtk.patch"
cmd="svn up \"$patch_file\""
$cmd
Run Code Online (Sandbox Code Playgroud)

注意“我的目录”中的空格。当我执行它时

$ ./test.sh 
Skipped '"/home/my'
Skipped 'dir/vtk.patch"'
Run Code Online (Sandbox Code Playgroud)

我不知道如何在变量中容纳空间并仍然执行命令。但是在bash shell上执行以下操作可以正常工作。

$ svn up "/home/my dir/vtk.patch"   #WORKS!!!
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激!我正在Windows上使用cygwin的bash。

variables bash space unicode-escapes

3
推荐指数
1
解决办法
6900
查看次数

如果它与其他模式不匹配,如何替换所有匹配的模式?

我想替换匹配模式的子字符串,只要它与不同的模式不匹配.例如,在下面显示的代码中,我想替换所有'%s'但保持':%s'不变.

String template1 = "Hello:%s";
String template2 = "Hello%s";

String regex = "[%s&&^[:%s]]";

String str = template1.replaceAll(regex, "");
System.out.println(str);

str = template2.replaceAll(regex, "");
System.out.println(str);
Run Code Online (Sandbox Code Playgroud)

输出应该是:

Hello:%s
Hello
Run Code Online (Sandbox Code Playgroud)

我在我的正则表达式中遗漏了一些东西.有线索吗?谢谢!

java regex replace

1
推荐指数
1
解决办法
885
查看次数