This program is to generate all possible strings from given char sequence with given set but it take much time, so how can I run it in multi cpu?
这个程序是使用给定的set从给定的char序列生成所有可能的字符串但是需要很长时间,所以如何在多cpu中运行它?
public class LapTest {
static int q=0;
public static void main(String[] args) {
System.out.println("First Test");
char set1[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int k = 4;
printAllKLength(set1, k);
System.out.println(q);
}
// The method that prints all possible strings of length k. It is
// mainly a wrapper over recursive function printAllKLengthRec()
static void printAllKLength(char set[], int k) {
int n = set.length;
printAllKLengthRec(set, "", n, k);
}
// The main recursive method to print all possible strings of length k
static void printAllKLengthRec(char set[], String prefix, int n, int k) {
// Base case: k is 0, print prefix
if (k == 0) { q++;
System.out.println(prefix);
if (prefix.equals("hkka")) {
System.exit(0);
}
return;
}
// One by one add all characters from set and recursively
// call for k equals to k-1
for (int i = 0; i < n; ++i) {
// Next character of input added
String newPrefix = prefix + set[i];
// k is decreased, because we have added a new character
printAllKLengthRec(set, newPrefix, n, k - 1);
}
}
}
2 个解决方案
#1
1
Your program is very likely I/O bound, this means writing the output to the screen will take more time than calculating the string. So processing in parallel will not help you in any way. No matter what you do with the string later on, on a modern computer, the sink (consumer of your string) is very likely slower than calculating it, even with an imperfect solution.
您的程序非常可能受I / O限制,这意味着将输出写入屏幕将比计算字符串花费更多时间。因此并行处理对您没有任何帮助。无论你以后如何处理字符串,在现代计算机上,即使使用不完美的解决方案,接收器(字符串的消费者)也很可能比计算它更慢。
Also for writing the string to screen, use a buffered writer which will make your program much faster. Define a BufferedOutputStream in your class:
另外,对于将字符串写入屏幕,请使用缓冲编写器,这将使您的程序更快。在类中定义BufferedOutputStream:
static BufferedOutputStream out = new BufferedOutputStream(System.out, 81920);
Then write with out.write((prefix+"\n").getBytes());
and don't forget to flush the stream with out.flush();
on both the end of the program and on the System.exit(0);
. This will give you a significant performance improvement.
然后用out.write写((前缀+“\ n”)。getBytes());并且不要忘记用out.flush()冲洗流。在程序结束和System.exit(0);.这将为您带来显着的性能提升。
Also try to not solve the problem without recursion, that might give you another speed pump.
也试着不解决没有递归的问题,这可能会给你另一个速度泵。
#2
0
There are many performance improvements you should make before you try to make it multi-threaded. I can see several obvious things you could do to improve performance. But experience has shown that most coders (myself included) are terrible at guessing where code should be tweaked to make it more efficient.
在尝试使其成为多线程之前,您应该进行许多性能改进。我可以看到你可以做的几件明显的事情来提高性能。但是经验表明,大多数编码人员(包括我自己)都在猜测应该调整代码的位置以使其更有效率。
So my advice is to learn to use a profiler. Profile your code and find out where it is spending cpu cycles then tweak the code in the right places. You will get much better results learning to do that. Good modern IDEs make this a pretty simple process for simple programmes.
所以我的建议是学习使用分析器。分析您的代码并找出它在cpu周期中花费的地方,然后在正确的位置调整代码。学习这样做会得到更好的结果。良好的现代IDE使这个简单的程序非常简单。
#1
1
Your program is very likely I/O bound, this means writing the output to the screen will take more time than calculating the string. So processing in parallel will not help you in any way. No matter what you do with the string later on, on a modern computer, the sink (consumer of your string) is very likely slower than calculating it, even with an imperfect solution.
您的程序非常可能受I / O限制,这意味着将输出写入屏幕将比计算字符串花费更多时间。因此并行处理对您没有任何帮助。无论你以后如何处理字符串,在现代计算机上,即使使用不完美的解决方案,接收器(字符串的消费者)也很可能比计算它更慢。
Also for writing the string to screen, use a buffered writer which will make your program much faster. Define a BufferedOutputStream in your class:
另外,对于将字符串写入屏幕,请使用缓冲编写器,这将使您的程序更快。在类中定义BufferedOutputStream:
static BufferedOutputStream out = new BufferedOutputStream(System.out, 81920);
Then write with out.write((prefix+"\n").getBytes());
and don't forget to flush the stream with out.flush();
on both the end of the program and on the System.exit(0);
. This will give you a significant performance improvement.
然后用out.write写((前缀+“\ n”)。getBytes());并且不要忘记用out.flush()冲洗流。在程序结束和System.exit(0);.这将为您带来显着的性能提升。
Also try to not solve the problem without recursion, that might give you another speed pump.
也试着不解决没有递归的问题,这可能会给你另一个速度泵。
#2
0
There are many performance improvements you should make before you try to make it multi-threaded. I can see several obvious things you could do to improve performance. But experience has shown that most coders (myself included) are terrible at guessing where code should be tweaked to make it more efficient.
在尝试使其成为多线程之前,您应该进行许多性能改进。我可以看到你可以做的几件明显的事情来提高性能。但是经验表明,大多数编码人员(包括我自己)都在猜测应该调整代码的位置以使其更有效率。
So my advice is to learn to use a profiler. Profile your code and find out where it is spending cpu cycles then tweak the code in the right places. You will get much better results learning to do that. Good modern IDEs make this a pretty simple process for simple programmes.
所以我的建议是学习使用分析器。分析您的代码并找出它在cpu周期中花费的地方,然后在正确的位置调整代码。学习这样做会得到更好的结果。良好的现代IDE使这个简单的程序非常简单。