这是否是所有编程语言都普遍存在的情况?做多个print后面跟一个println似乎更快,但将所有内容移动到字符串中并仅打印该字符串似乎最快。为什么?
编辑:例如,Java可以在不到一秒钟的时间内找到1百万以内的所有质数-但逐个打印它们上一个单独的println可能需要几分钟!打印10亿个数字可能需要几个小时!
例子:
package sieveoferatosthenes;
public class Main {
public static void main(String[] args) {
int upTo = 10000000;
boolean primes[] = new boolean[upTo];
for( int b = 0; b < upTo; b++ ){
primes[b] = true;
}
primes[0] = false;
primes[1] = false;
int testing = 1;
while( testing <= Math.sqrt(upTo)){
testing ++;
int testingWith = testing;
if( primes[testing] ){
while( testingWith < upTo ){
testingWith = testingWith + testing;
if ( testingWith >= upTo){
}
else{
primes[testingWith] = false;
}
}
}
}
for( int b = 2; b < upTo; b++){
if( primes[b] ){
System.out.println( b );
}
}
}
}
println
和不打印输出哪个,还是println
和print
之间的区别?它们是非常不同的问题。 - Josh Lee