zoukankan      html  css  js  c++  java
  • 2020年秋第三周-白名单

    此作业的要求参见[https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11207]

    作业0-命令行参数确定生成的数据的数据量

    修改后的代码

    //create.cpp
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	if (argc != 2) { return 0; }
    	int num = atoi(argv[1]);        //输入形式create 100 > file
    
    	srand((unsigned)time(NULL));
    	for (int i = 0; i < num; i++)
    	{
    		cout << rand() << "
    ";
    	}
    	cout << endl;
    	return 0;
    }
    

    修改后的readme.md

    1. 安装vs2019;
    2. 配置环境变量,配置C++编译环境;
    3. 编译create.cpp文件,生成可执行文件create;
    4. 执行“create 10 > whitelist”生成文件whitelist;
    5. 执行“create 1000 > q”生成文件q;
    6. 编译brute.cpp文件,生成brute可执行文件;
    7. 执行“brute -w q < whitelist > output”,获得白名单output
    

    作业1-对C#那段代码进行profile

    profile结果如下,观察发现System.Console.WriteLine(int32)占用时间高达98.63%

    生成包含1M个数据的biggerwhitelist和10M个数据的biggerq,执行结果如下图

    部署运行代码的readme

    1. 安装vs2019;
    2. 配置环境变量,C++开发环境和C#开发环境;
    3. 编译create.cpp文件,生成create可执行文件;
    4. 执行“create 1000000 > biggerwhitelist”生成文件biggerwhitelist;
    5. 执行“create 10000000 > biggerq”生成文件biggerq;
    6. 编译brute.cpp文件;
    7. 执行“foo biggerq < biggerwhitelist > biggeroutput”,获得白名单biggeroutput
    

    作业2-以biggerwhitelist和biggerq作为输入,对作业1中选择的代码再次进行profile

    以biggerwhitelist和biggerq作为输入,进行profile,System.Console.WriteLine(int32)占用时间高达97.82%

    作业3-根据作业2找到的最慢的地方,优化作业1中你选择的代码

    优化后的代码如下
    主函数

    static void Main(string[] args)
            {
                DateTime beforDT = System.DateTime.Now;
                if (args.Count() < 1)
                    return;
                string path = args[0];
                string[] sm = File.ReadAllLines(path);                      //read q file
                int[] array = new int[1000000];
    
                int index = 0;
                string strNum;
                while ((strNum = Console.ReadLine().Trim()).Length > 0)     //read witelist file
                {
                    array[index] = Convert.ToInt32(strNum);
                    index++;
                }
    
                string str = "";
                for (int i = 1; i < sm.Length && sm[i].Length > 0; ++i)
                {
                    int temp = Convert.ToInt32(sm[i]);
                    if (find(temp, array) == -1)
                        //Console.WriteLine(temp);
                        str += temp.ToString() + "
    ";
                }
                Console.Write(str);
                DateTime afterDT = System.DateTime.Now;
                TimeSpan ts = afterDT.Subtract(beforDT);
                Console.WriteLine("DateTime: {0}ms.", ts.TotalMilliseconds);
            }
    

    find函数

    static int find(int key, int[] array)
            {
                for (int j = 0; j < array.Length; j++)
                {
                    if (key == array[j])
                        return key;
                }
                return -1;
            }
    

    代码GitHub地址为https://github.com/1501106169/nenuWork.git

    作业4-对作业3优化后的代码进行profile,结果与作业2的结果做对比

    由于未优化的代码通过每次调用System.Console.WriteLine()进行输出,故作优化为一次性输出

    作业5-注释和代码风格

    1)、注释少,关键语句未打注释
    2)、代码中存在错误,如未对数据进行判断就进行数值转化(读取文件末尾的空字符串转化为整形是报错)
    3)、readme.md文件写的不够清楚

    面试结束了,我想对老杨说,
    你的努力和付出我们有目共睹,但是编码能力和代码规范有待提高,你对需求的实现较完整,你被录取了,但不要因此而懈怠。

  • 相关阅读:
    2019/9/8
    实现简单的网页登录注册功能 (使用html和css以及javascript技术) 没有美化的日后补全
    测试一些以前的代码
    使用三层开发遵循的原则
    超市管理
    热身训练
    考试第三题
    考试第七题
    考试第10题
    考试第8题
  • 原文地址:https://www.cnblogs.com/HanYG/p/13700660.html
Copyright © 2011-2022 走看看