From 5e9b53220f87b9f251b084526dd5b46f40043bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=A8=9F?= <3113368166@qq.com> Date: Mon, 20 Feb 2023 03:29:59 +0000 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=EF=BC=8C=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 谢娟 <3113368166@qq.com> --- files/homework.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 files/homework.js diff --git a/files/homework.js b/files/homework.js new file mode 100644 index 0000000..4959b57 --- /dev/null +++ b/files/homework.js @@ -0,0 +1,41 @@ +'use strict'; +let fs = require("fs"); +let StartTime = new Date().getTime(); +if(!fs.existsSync("tb1")) fs.mkdirSync("tb1"); +for (let i = 1; i < 1001; i++) { + fs.writeFileSync("./tb1/" + i + ".txt", "hello"); +} +let EndTime = new Date().getTime(); +console.log("同步耗时为" + (EndTime - StartTime)); +//异步耗时 +var StartTime1 = new Date().getTime(); +if(!fs.existsSync("tb2")) fs.mkdirSync("tb2"); + +var Maxnum = 0; +for (var i = 1; i < 1001; i++) { + fs.writeFile("./tb2/" + i + ".txt", "hi", (err, data) => { + var EndTime1 = new Date().getTime(); + if (Maxnum < (EndTime1 - StartTime1)) { + Maxnum = (EndTime1 - StartTime1); + } + }); + +} + +setTimeout(function () { + console.log("异步耗时为:" + Maxnum); +}, 2000) +for (var a = 1; a < 1001; a++) { + fs.readFile("./tb1/" + a + ".txt", 'utf-8', (err, data) => { + console.log(data.toString()); + }) + fs.readFile("./tb1/" + a + ".txt", 'ascii', (err, data) => { + console.log(data.toString()); + }) + fs.readFile("./tb2/" + a + ".txt", 'utf-8', (err, data) => { + console.log(data.toString()); + }) + fs.readFile("./tb2/" + a + ".txt", 'ascii', (err, data) => { + console.log(data.toString()); + }) +} \ No newline at end of file -- Gitee