• 随笔,关于生成随机数,排序


    生成N个随机数,N<100W,不能重复

    Console.WriteLine(DateTime.Now.ToLongTimeString());
    Random random = new Random();
    Byte[] bytes = new Byte[1000000];
    StreamWriter sw = new StreamWriter("Values.txt", true);
    for (int i = 0; i < 1000000; i++){
        var result = random.Next(0, 1000000);
        if(bytes[result] == 1) 
        {
            continue;
        }
        bytes[i] = 1;
        sw.WriteLine(result);
        sw.Flush();
    }
    sw.Close();
    Console.WriteLine(DateTime.Now.ToLongTimeString());
    Console.Read();

    接上题,对N个随机数进行排序,N<100W

    StreamReader sr = new StreamReader("Values.txt");
    Byte[] bytes = new Byte[1000000];
    Console.WriteLine(DateTime.Now.ToLongTimeString());
    while (!sr.EndOfStream)
    {
        bytes[Int32.Parse(sr.ReadLine())] = 1;
    }
    sr.Close();
    
    StreamWriter sw = new StreamWriter("Values2.txt", true);
    for (int i = 0; i < 1000000; i++)
    {
        if(bytes[i] == 1) 
        {
            sw.WriteLine(i);
            sw.Flush();
        }
    }
    sw.Close();
    Console.WriteLine(DateTime.Now.ToLongTimeString());
    Console.Read();

    整个排序过程耗时2秒,大约70W条记录

  • 相关阅读:
    1.17 Python基础知识
    反射
    面向对象——类
    异常处理
    面向对象——静态方法、类方法、属性法法,其他特殊方法
    面向对象——继承
    question
    configparser模块
    hashlib模块
    shelve模块
  • 原文地址:https://www.cnblogs.com/warrior/p/3678682.html
Copyright © 2020-2023  润新知