java vs c 性能_性能问题:Java vs C

我一直听说C比Java更有效率(这就是为什么大多数游戏是用C开发的)。

我写了一个小算法来解决Java和C中的“八皇后拼图”,使用完全相同的算法,然后开始提高数字或正方形。

当达到20 * 20甚至22 * 22的检查板时,Java显然更有效(C为3秒对66秒)。

我不知道为什么,但我很开始与C,所以有可能我犯了一些巨大的性能错误,所以我会很乐意接受任何信息,将帮助我了解发生了什么。

下面是我在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 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;

}

}

下面是C中的代码:

#include

#include

#include

#include

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 positions;

bool verifierNonPrise(Point emplacement) {

bool nonPrise = true;

for (list::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::iterator it = positions.begin(); it!= positions.end(); it++) {

cout << "(" << it->x() << "; " << it->y() << ")" << endl;

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值