大字符串列表应该是不可能的
large list of strings should be impossible
我在存储字符串列表的 C# 控制台应用程序中有 运行 代码。这段代码的objective是在1秒内尽可能创建字符串"q"的最大集合。纯属锻炼自己的编程能力,没有实际应用。
当我 运行 这段代码时,它会在 1 秒内自行停止,当我计算所有字符串中的所有 "q" 时,我得到 214,870,505,313,584,这是数百万亿. char "q" 占用一个字节,如果这个东西有 200 万亿字节,则意味着字符串列表超过 2 TB。
这怎么可能,是否正在进行某种自动压缩?有没有办法关闭它?
这里是代码,如果你想要的话。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleAppWebScrape
{
class Program
{
//the origional string
static string l = "q";
static DateTime d;
static List<string> output = new List<string>();
static void Main(string[] args)
{
//hit enter to start
Console.Read();
d = DateTime.Now;
//start the string doubling thread
Thread thred2 = new Thread(new ThreadStart(doubleL));
thred2.Start();
//start the write to list thread
Thread thred1 = new Thread(new ThreadStart(writeToFile));
thred1.Start();
while (haveTime())
{
//pause current thread for the remander of the second.
}
long lo = howmany();
Console.WriteLine(lo);
Console.ReadLine();
}
//determines the amount of "q"s in the list of strings
static long howmany()
{
long lo = 0;
foreach (string s in output)
{
lo += s.Length;
}
return lo;
}
//writes to the list of strings.
static void writeToFile()
{
while (haveTime())
{
output.Add(l);
}
}
//builds a string by doubling the origional string
static bool tobig = false;
static void doubleL()
{
while (haveTime() && !tobig)
{
if (l.Length < 268435456)
{
l = l + l;
}
else
{
tobig = true;
}
}
}
//bool to determin if it is running inside one second
static bool haveTime()
{
if ((DateTime.Now - d).TotalSeconds < 1)
{
return true;
}
return false;
}
}
}
我前阵子读到字符串存储在堆上并被引用。由于它一遍又一遍地是同一个字符,您可能正在为同一个物理地址创建数万亿个引用。
我在存储字符串列表的 C# 控制台应用程序中有 运行 代码。这段代码的objective是在1秒内尽可能创建字符串"q"的最大集合。纯属锻炼自己的编程能力,没有实际应用。
当我 运行 这段代码时,它会在 1 秒内自行停止,当我计算所有字符串中的所有 "q" 时,我得到 214,870,505,313,584,这是数百万亿. char "q" 占用一个字节,如果这个东西有 200 万亿字节,则意味着字符串列表超过 2 TB。
这怎么可能,是否正在进行某种自动压缩?有没有办法关闭它?
这里是代码,如果你想要的话。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleAppWebScrape
{
class Program
{
//the origional string
static string l = "q";
static DateTime d;
static List<string> output = new List<string>();
static void Main(string[] args)
{
//hit enter to start
Console.Read();
d = DateTime.Now;
//start the string doubling thread
Thread thred2 = new Thread(new ThreadStart(doubleL));
thred2.Start();
//start the write to list thread
Thread thred1 = new Thread(new ThreadStart(writeToFile));
thred1.Start();
while (haveTime())
{
//pause current thread for the remander of the second.
}
long lo = howmany();
Console.WriteLine(lo);
Console.ReadLine();
}
//determines the amount of "q"s in the list of strings
static long howmany()
{
long lo = 0;
foreach (string s in output)
{
lo += s.Length;
}
return lo;
}
//writes to the list of strings.
static void writeToFile()
{
while (haveTime())
{
output.Add(l);
}
}
//builds a string by doubling the origional string
static bool tobig = false;
static void doubleL()
{
while (haveTime() && !tobig)
{
if (l.Length < 268435456)
{
l = l + l;
}
else
{
tobig = true;
}
}
}
//bool to determin if it is running inside one second
static bool haveTime()
{
if ((DateTime.Now - d).TotalSeconds < 1)
{
return true;
}
return false;
}
}
}
我前阵子读到字符串存储在堆上并被引用。由于它一遍又一遍地是同一个字符,您可能正在为同一个物理地址创建数万亿个引用。