我一直听说,C ++是方式比Java更有效(这就是为什么大多数游戏都developped在C ++中)。
我写了一个小的算法来解决Java和C ++中的八皇后问题,使用完全相同的算法,然后开始饲养的数量或正方形。 当到达的20 * 20或甚至22 * 22 checkboards,看来Java是更为有效的(3秒VS 66秒用于C ++)
我不知道为什么,但我pretty的使用C ++开始,所以有可能我做了一些巨大的性能错误,所以我会很乐意接受任何信息,这将有助于我明白发生了什么。
下面是code我在Java中使用:
进口java.awt.Point中;
进口的java.util.ArrayList;
进口的java.util.List;
公共类HuitDames {
/ **
*拉清单当然DEScoordnnéesDES贵妇人。
* /
私有静态列表<点>位置=新的ArrayList<>();
/ **
* Largeur德拉格栅。
* /
私有静态最终诠释LARGEUR_GRILLE = 22;
/ **
*参数ARGS的命令行参数
* /
公共静态无效的主要(字串[] args){
INT I = 1;
placerDame(ⅰ);
对于(点点:位置){
的System.out.println((+ point.x +;+ point.y +));
}
}
/ **
*将UNE圣母院等返回true SI LA位置EST保姆。
*参数我乐NUMERO德拉贵妇人。
* @返回SI LA位置EST保姆。
* /
私有静态布尔placerDame(int i)以{
布尔bonnePosition = FALSE;
对于(INT J = 1; J< = LARGEUR_GRILLE和放大器;&安培; bonnePosition ==假; J ++){
点进驻=新的点(I,J);
positions.add(进驻);
如果(verifierPrise(炮位)及&安培;(ⅰ== LARGEUR_GRILLE || placerDame第(i + 1))){
bonnePosition = TRUE;
}
其他 {
positions.remove(ⅰ - 1);
}
}
返回bonnePosition;
}
/ **
*Vérifie阙拉新式的位置n'est PAS恩奖AVEC UNE位置似曾相识présente。
*参数的位置拉的位置德拉新式的贵妇人。
* @返回思啦位置面值无限便捷融洽辅助位置DES其他残疾贵妇人。
* /
私有静态布尔verifierPrise(点位){
布尔nonPrise = TRUE;
对于(点点:位置){
如果(!point.equals(位置)){
// CAS大全MSDSOù拉河畔即使得到Colonne酒店。
如果(position.y == point.y){
nonPrise = FALSE;
}
// CAS大全MSDSOù河畔即使得到diagonale。
如果(Math.abs(position.y - point.y)== Math.abs(position.x - point.x)){
nonPrise = FALSE;
}
}
}
返回nonPrise;
}
}
和下方则是code C ++中:
的#include<的iostream>
#包括<列表>
#包括<文件math.h>
#包括< stdlib.h中>
使用名字空间std;
//类重新present点。
类Point {
私人:
双XVAL,利用yval;
上市:
//构造函数使用默认参数,可以调用零,一,
//或两个值。
点(双X = 0.0,双Y = 0.0){
XVAL = X;
利用yval = Y;
}
//提取器。
双X(){返回XVAL; }
双Y(){返回利用yval; }
};
#定义LARGEUR_GRILLE 22
名单<点>位置;
布尔verifierNonPrise(点进驻){
布尔nonPrise = TRUE;
对于(名单<点>:迭代它= positions.begin(!);它= positions.end();它++){
如果(它 - >!X()= emplacement.x()){
如果(它 - > Y()== emplacement.y()){
nonPrise = FALSE;
}
如果(绝对(它 - >γ() - emplacement.y())==无水(它 - &X的催化剂() - emplacement.x())){
nonPrise = FALSE;
}
}
}
返回nonPrise;
}
布尔placerDame(int i)以{
布尔bonnePosition = FALSE;
对于(INT J = 1; J< = LARGEUR_GRILLE和放大器;&安培;!bonnePosition; J ++){
安置点(I,J);
positions.push_back(进驻);
如果(verifierNonPrise(炮位)及&安培;(ⅰ== LARGEUR_GRILLE || placerDame第(i + 1))){
bonnePosition = TRUE;
}
其他 {
positions.pop_back();
}
}
返回bonnePosition;
}
诠释的main()
{
INT I = 1;
placerDame(ⅰ);
对于(名单<点>:迭代它= positions.begin(!);它= positions.end();它++){
COUT<< (&其中;&其中;它 - &X的催化剂()&其中;&所述;;&其中;&其中;它 - > Y()&其中;&所述;)&其中;&其中; ENDL;
}
返回0;
}
解决方案
的std ::列表
在C ++中是一个链表,而的Java。 util.ArrayList
是一个数组。尝试更换的std ::列表
按的std ::矢量
。此外,一定要编译优化开启。
I have always heard that C++ was way more efficient than Java (and that is why most games are developped in C++).
I wrote a small algorithm to solve the "Eight queens puzzle" in both Java and C++, using the exact same algorithm, and then started to raise the number or squares. When reaching checkboards of 20*20 or even 22*22, it appears Java is much more effective (3 seconds vs 66 seconds for C++).
I have no idea why, but I am pretty beginning with C++, so it is possible I made some huge performance mistakes, so I will gladly accept any information that would help me understand what is happening.
Below is the code I use in Java:
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
public class HuitDames {
/**
* La liste des coordnnées des dames.
*/
private static List<Point> positions = new ArrayList<>();
/**
* Largeur de la grille.
*/
private static final int LARGEUR_GRILLE = 22;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int i = 1;
placerDame(i);
for (Point point : positions) {
System.out.println("(" + point.x + "; " + point.y + ")");
}
}
/**
* Place une dame et return true si la position est bonne.
* @param i le numéro de la dame.
* @return si la position est bonne.
*/
private static boolean placerDame(int i) {
boolean bonnePosition = false;
for (int j = 1; j <= LARGEUR_GRILLE && bonnePosition == false; j++) {
Point emplacement = new Point(i, j);
positions.add(emplacement);
if (verifierPrise(emplacement) && (i == LARGEUR_GRILLE || placerDame(i + 1))) {
bonnePosition = true;
}
else {
positions.remove(i - 1);
}
}
return bonnePosition;
}
/**
* Vérifie que la nouvelle position n'est pas en prise avec une position déjà présente.
* @param position la position de la nouvelle dame.
* @return Si la position convient par rapport aux positions des autres dames.
*/
private static boolean verifierPrise(Point position) {
boolean nonPrise = true;
for (Point point : positions) {
if (!point.equals(position)) {
// Cas où sur la même colonne.
if (position.y == point.y) {
nonPrise = false;
}
// Cas où sur même diagonale.
if (Math.abs(position.y - point.y) == Math.abs(position.x - point.x)) {
nonPrise = false;
}
}
}
return nonPrise;
}
}
And below is the code in C++:
#include <iostream>
#include <list>
#include <math.h>
#include <stdlib.h>
using namespace std;
// Class to represent points.
class Point {
private:
double xval, yval;
public:
// Constructor uses default arguments to allow calling with zero, one,
// or two values.
Point(double x = 0.0, double y = 0.0) {
xval = x;
yval = y;
}
// Extractors.
double x() { return xval; }
double y() { return yval; }
};
#define LARGEUR_GRILLE 22
list<Point> positions;
bool verifierNonPrise(Point emplacement) {
bool nonPrise = true;
for (list<Point>::iterator it = positions.begin(); it!= positions.end(); it++) {
if (it->x() != emplacement.x()) {
if (it->y() == emplacement.y()) {
nonPrise = false;
}
if (abs(it->y() - emplacement.y()) == abs(it->x() - emplacement.x())) {
nonPrise = false;
}
}
}
return nonPrise;
}
bool placerDame(int i) {
bool bonnePosition = false;
for (int j = 1; j <= LARGEUR_GRILLE && !bonnePosition; j++) {
Point emplacement(i,j);
positions.push_back(emplacement);
if (verifierNonPrise(emplacement) && (i == LARGEUR_GRILLE || placerDame(i + 1))) {
bonnePosition = true;
}
else {
positions.pop_back();
}
}
return bonnePosition;
}
int main()
{
int i = 1;
placerDame(i);
for (list<Point>::iterator it = positions.begin(); it!= positions.end(); it++) {
cout << "(" << it->x() << "; " << it->y() << ")" << endl;
}
return 0;
}
解决方案
std::list
in C++ is a linked list, whereas java.util.ArrayList
is an array. Try replacing std::list
by std::vector
. Also, be sure to compile with optimization turned on.