- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要一种更快的方法来将 160 万位的 BigInteger 输出到文件。我现在正在使用这段代码。
FileStream fs1 = new FileStream("C:\\Output\\Final\\BigInteger.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs1);
writer.WriteLine(big);
writer.Close();
输出 160 万位数字大约需要 5 分钟。有什么办法可以加快速度吗?
最佳答案
这是一个非常愚蠢的问题,没有实际用途。但准确了解处理器周期的使用位置始终很重要。您提示写入文件花费的时间太长。好吧,您确定它实际上慢的是文件吗?还是 BigInteger.ToString() 太慢了?
找出问题的最佳方法就是写入文件,这样您就可以隔离问题:
using System;
using System.Text;
using System.IO;
class Program {
static void Main(string[] args) {
var big = new StringBuilder(1600 * 1000);
big.Append('0', big.Capacity);
var sw = System.Diagnostics.Stopwatch.StartNew();
// Your code here
FileStream fs1 = new FileStream("BigInteger.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs1);
writer.WriteLine(big);
writer.Close();
// End of your code
sw.Stop();
Console.WriteLine("That took {0} milliseconds", sw.ElapsedMilliseconds);
Console.ReadLine();
}
}
我机器上的输出:
That took 13 milliseconds
写入文件非常快,文件系统缓存使其成为内存到内存的副本。在您的程序停止运行很久之后,操作系统才将其延迟写入磁盘。当您写入的数据超出缓存中的容量时,它永远无法隐藏缓慢的磁盘写入速度。在任何现代机器上你都离不开它,它们有很多 RAM 并且可以轻松存储 1 GB。 1.6 兆字节是牙线。
所以您知道实际上是 BigInteger.ToString() 太慢了。是的。它将 Big Mother 存储在基数 2 中,使数学运算尽可能快。像 base 2 这样的处理器,他们用 2 个手指计数。转换为以 10 为底的人类格式,这很昂贵。它需要除法,这是使用处理器所能做的最昂贵的事情之一。
关于c# - 快速将非常大的 BigInteger 写入 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33377766/
我正在尝试计算一个大数,这需要 BigInteger.Pow(),但我需要指数也是 BigInteger 而不是 整数。 即 BigInteger.Pow(BigInteger) 我怎样才能做到这一点
在 Java 中,大多数基本类型都是有符号的(一位用于表示 +/-),因此当我超出类型的限制时,我会得到意想不到的结果,比如负数。 有没有比使用 BigInteger 更好的解决方案,因为 BigIn
如何选择随机元素 α ∈ Z*p? P 是一个随机的 1024 位素数 BigInteger。 这是我找到 BigInteger p 的方法: Random rand = new Random(new
我正在尝试使用 BigInteger 类在 Java 中实现 Fermat、Miller-Rabin 或 AKS 算法。 我想我有Fermat test已实现,但 BigInteger 类不允许将 B
我知道 BigInteger 有一个构造函数,您可以在其中通过传递新 BigInteger 的最大 bitLength 和一个随机参数来生成随机 BigInteger: BigInteger(int
我正在考虑为自己存储公钥的想法。为此,我需要将 BigInteger 转换为某种变量,然后从该值重新创建 BigInteger。 我在 Stackoverflow 中进行了搜索,找到了执行此操作的最佳
目前我正在使用 Long 整数类型。我使用以下方法从/到二进制/数字进行转换: Convert.ToInt64(BinaryString, 2); //Convert binary string of
我有以下代码: public static BigInteger[] Cubes (int m){ Set result = new HashSet(); for (int i = 1
因此,在我的计算机科学课上,我们一直在学习如何使用 Java 编码,并且我遇到了编码本身的第一个局限性。问题是 long 只允许您存储最多 64 位或接近该位的数字。所以我们已经开始用 16 位数字进
你好我想计算 2^(256bit number) 在 java 中,但 biginteger 的 pow 函数只能处理 int。 如何计算更大的数字? 有图书馆吗? 我想计算来自的所有数字 2^0 2
我正在尝试构建一个 BigInteger 数组,但似乎该数组需要由整数本身索引(如果是这样,那对我来说似乎非常愚蠢,但我希望我只是误解了一些东西) .我正在尝试的基本上是以下内容: BigIntege
我在 .Net 项目中使用 BigInteger ( link ) 的单声道实现在 Java 中我使用 java.math.BigInteger。 相同的代码在 Java 中产生不同的结果。 .Net
我正在构建 DSA 算法。但是在将 BigInteger 数字与其他 BigInteger 数字进行排名时遇到了问题。这是我要使用的公式: v = ((g^u1 * y^u2) mod p) mod
我正在尝试用Java实现Schnorr签名算法。我遇到了计算大指数幂(例如 MD5 哈希值)的问题。 有什么方法可以让BigInteger获得BigInteger的权力吗? 我需要计算 (a^x*b^
我正在编写斐波那契程序。 public class ImperativeFibonacci implements Fibonacci { public BigInteger fibonacci(i
我原以为 BigInteger 类中的两个构造函数 BigInteger(String) 和 BigInteger(byte[]) 的行为相似,但事实并非如此。 为什么两个 BigInteger 不相
在 Java 中,要将 String 转换为 BigInteger,您可以使用构造函数 new BigInteger(String),但要将 int/long 转换为工厂函数,您可以使用 BigInt
我正在做一些大整数计算,我需要对一个 BigInteger 求另一个 BigInteger 的幂。 .pow() 方法执行我想要的操作,但将 int 值作为参数。 .modPow 方法采用 BigIn
Java 7 方法的复杂性是什么pow和 isProbablePrime在BigInteger类(class)? 我知道 Rabin 测试的简单实现具有 O(k(log(n))^3) 复杂度,可以通过
.NET 4.0 为任意大的整数提供了 System.Numerics.BigInteger 类型。我需要计算 BigInteger 的平方根(或合理的近似值——例如整数平方根)。这样我就不必重新实现
我是一名优秀的程序员,十分优秀!