gpt4 book ai didi

c# - 暴力破解性能 : Java vs C#

转载 作者:行者123 更新时间:2023-11-30 22:31:12 25 4
gpt4 key购买 nike

<分区>

编辑:我会尝试更好地说明我的问题。我不是在问如何衡量性能或任何东西或如何编写算法。我尝试将 Java 应用程序的源代码镜像到 C# 中。 Java 代码的运行速度比 C# 代码快 1.3 - 2.0 倍。那么为什么会这样呢?我在移植代码时犯了错误吗? java 中ExecutorService 的线程是否与c# 任务相同?为什么 java 单线程甚至比 c# 多线程更快(java 多线程是最快的)?

出于测试目的(回到语言),我用 Java 编写了一个伪暴力应用程序,然后将其移植到 C#。我试图尽可能少地改变,以便两个来源在语义上保持相同。我知道这些代码片段并不完美,请不要尝试更正其背后的算法,因为这与问题无关。

问题:因此,当我连续运行两个应用程序然后比较输出时,Java 在每次尝试中都更快,而 Java 单线程几乎与 C# 多线程一样快或更快。我想知道为什么会这样,也许我在 C# 版本的代码中做错了什么(如果有的话)。关于致命编码错误的任何提示?此外,在 Windows XP 配置上,C# 多线程比 C# 单线程慢(这怎么可能?)。

尝试了两种配置:

1)
Windows 7 x64
i7 cpu, 8 cores ( 4 physical cores + Hyperthreading )
.Net 4.0 and jdk 7

2)
Windows XP x86
Atom N270, 2 cores ( 1 physical core + Hyperthreading )
.Net 4.0 and jdk 7

我把代码贴出来,你可以自己测试。

Java 代码:

入口.java

package test;
import java.util.Scanner;

public class Entry
{
public static void main(String[] args)
{

System.out.print("Type password to be cracked: ");
String input = new Scanner(System.in).nextLine();
PasswordCracker cracker = new PasswordCracker();
System.out.println("Multithreaded");
cracker.runMulti(input);
cracker = new PasswordCracker();
System.out.println("Singlethreaded");
cracker.runSingle(input);
System.out.println("Finished...");
}
}

密码破解器.java

package test;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class PasswordCracker
{

String passwordToCrack;
public boolean passwordFound;
int min;
int max;
StringBuilder crackedPassword;

public void prepare(String text)
{
passwordToCrack = text;

passwordFound = false;
min = 32;
max = 126;
crackedPassword = new StringBuilder();
crackedPassword.append((char) (min - 1));
}

public void result()
{
System.out.println("Cracked Password is: " + crackedPassword.toString());
}

public void incrementString(StringBuilder text, int min, int max)
{
text.setCharAt(0, (char) ((int) text.charAt(0) + 1));
for (int i = 0; i < text.length(); i++)
{
if (text.charAt(i) > (char) max)
{
text.setCharAt(i, (char) min);
if (text.length() == i + 1)
{
text.append((char) min);
}
else
{
text.setCharAt(i + 1, (char) ((int) text.charAt(i + 1) + 1));
}
}
}
}

public void runMulti(String text)
{
prepare(text);
double time = System.nanoTime();
doItMulti();
time = System.nanoTime() - time;
System.out.println(time / (1000000000));
result();

}

public void runSingle(String text)
{
prepare(text);
double time = System.nanoTime();
doItSingle();
time = System.nanoTime() - time;
System.out.println(time / (1000000000));
result();
}

public void doItSingle()
{
while (passwordFound == false)
{
incrementString(crackedPassword, min, max);
passwordFound = crackedPassword.toString().equals(passwordToCrack);
}
}

public void doItMulti()
{
int cores = Runtime.getRuntime().availableProcessors();
ArrayList<Future<?>> tasks = new ArrayList<Future<?>>(cores);
ExecutorService executor = Executors.newFixedThreadPool(cores);
final long step = 2000;
for (long i = 0; i < Long.MAX_VALUE; i += step)
{
while(tasks.size() > cores)
{
for(int w = 0; w < tasks.size();w++)
{
if(tasks.get(w).isDone())
{
tasks.remove(w);
break;
}
}
try
{
Thread.sleep(0);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
{
final long j = i;
if (passwordFound == false)
{
tasks.add(executor.submit(new Runnable()
{

public void run()
{
long border = j + step;
StringBuilder toCrack = new StringBuilder(10);
toCrack.append(constructString3(j, min, max));
for (long k = j; k < border; k++)
{
incrementString(toCrack, min, max);
boolean found = toCrack.toString().equals(passwordToCrack);
if (found)
{
crackedPassword = toCrack;
passwordFound = found;
break;
}
}
}
}));
}
else
{
break;
}
}
}
executor.shutdownNow();
}

public String constructString3(long number, long min, long max)
{
StringBuilder text = new StringBuilder();
if (number > Long.MAX_VALUE - min)
{
number = Long.MAX_VALUE - min;
}
ArrayList<Long> vector = new ArrayList<Long>(10);
vector.add(min - 1 + number);
long range = max - min + 1;
boolean nextLetter = false;
for (int i = 0; i < vector.size(); i++)
{
long nextLetterCounter = 0;
while (vector.get(i) > max)
{
nextLetter = true;
long multiplicator = Math.abs(vector.get(i) / range);
if ((vector.get(i) - (multiplicator * range)) < min)
{
multiplicator -= 1;
}
vector.set(i, vector.get(i) - (multiplicator * range));
nextLetterCounter += multiplicator;
}
if (nextLetter)
{
vector.add((long) (min + nextLetterCounter - 1));
nextLetter = false;
}
text.append((char) vector.get(i).intValue());
}
return text.toString();
}

和 C# 代码:

入门.cs

using System;

namespace PasswordCracker
{
class Entry
{
public static void Main(String[] args)
{
Console.Out.WriteLine("Type password to be cracked:");
String input = Console.In.ReadLine();
PasswordCracker cracker = new PasswordCracker();
Console.Out.WriteLine("Multithreaded");
cracker.runMulti(input);
cracker = new PasswordCracker();
Console.Out.WriteLine("Singlethreaded");
cracker.runSingle(input);
Console.Out.WriteLine("Finished...");
Console.ReadKey();
}
}
}

密码破解器.cs

using System;
using System.Collections.Generic;
using System.Text;

using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;

namespace PasswordCracker
{

public class PasswordCracker
{

String passwordToCrack;
public bool passwordFound;
int min;
int max;
StringBuilder crackedPassword;

public void prepare(String text)
{
passwordToCrack = text;

passwordFound = false;
min = 32;
max = 126;
crackedPassword = new StringBuilder();
crackedPassword.Append((char)(min - 1));
}

public void result()
{
Console.Out.WriteLine("Cracked Password is: " + crackedPassword.ToString());
}

public void incrementString(StringBuilder text, int min, int max)
{
text[0] = (char)((text[0]) + 1);
for (int i = 0; i < text.Length; i++)
{
if (text[i] > (char)(max))
{
text[i] = (char)(min);
if (text.Length == i + 1)
{
text.Append((char)(min));
}
else
{
text[i + 1] = (char)((text[i + 1]) + 1);
}
}
}
}

public void runMulti(String text)
{
prepare(text);
Stopwatch time = new Stopwatch();
time.Start();
doItMulti();
Console.Out.WriteLine(time.Elapsed.TotalSeconds);
result();

}

public void runSingle(String text)
{
prepare(text);
Stopwatch time = new Stopwatch();
time.Start();
doItSingle();
Console.Out.WriteLine(time.Elapsed.TotalSeconds);
result();
}

public void doItSingle()
{
while (passwordFound == false)
{
incrementString(crackedPassword, min, max);
passwordFound = crackedPassword.ToString().Equals(passwordToCrack);
}
}

public void doItMulti()
{
int cores = Environment.ProcessorCount;
long step = 2000;
List<Task> tasks = new List<Task>(cores);
for (long i = 0; i < long.MaxValue; i += step)
{
while (tasks.Count > cores)
{
for (int a = 0; a < tasks.Count;a++)
{
if (tasks[a].IsCompleted)
{
tasks.RemoveAt(a);
break;
}
}
Thread.Sleep(0);
}
{
long j = i;
if (passwordFound == false)
{
tasks.Add(Task.Factory.StartNew(delegate
{
long border = j + step;
StringBuilder toCrack = new StringBuilder(10);
toCrack.Append(constructString3(j, min, max));
for (long k = j; k < border; k++)
{
incrementString(toCrack, min, max);
bool found = toCrack.ToString().Equals(passwordToCrack);
if (found)
{
crackedPassword = toCrack;
passwordFound = found;
break;
}
}
}));
}
else
{
break;
}
}
}
}

public String constructString3(long number, long min, long max)
{
StringBuilder text = new StringBuilder();
if (number > long.MaxValue - min)
{
number = long.MaxValue - min;
}
List<long> vector = new List<long>(10);
vector.Add(min - 1 + number);
long range = max - min + 1;
bool nextLetter = false;
for (int i = 0; i < vector.Count; i++)
{
long nextLetterCounter = 0;
while (vector[i] > max)
{
nextLetter = true;
long multiplicator = Math.Abs(vector[i] / range);
if ((vector[i] - (multiplicator * range)) < min)
{
multiplicator -= 1;
}
vector[i] = vector[i] - (multiplicator * range);
nextLetterCounter += multiplicator;
}
if (nextLetter)
{
vector.Add((min + nextLetterCounter - 1));
nextLetter = false;
}
text.Append((char)(vector[i]));
}
return text.ToString();
}
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com