Skip to content

Instantly share code, notes, and snippets.

@ivanoats
Created March 4, 2020 20:53
Show Gist options
  • Save ivanoats/31e7cea617ceaf73c430fd1124c3eb17 to your computer and use it in GitHub Desktop.
Save ivanoats/31e7cea617ceaf73c430fd1124c3eb17 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/rinegik
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/* develop a program to calculate the time taken by each function in a performance log file
the format of the log file is given below
f1 start 0
f2 start 100
f3 start 200
f3 end 300
f2 end 400
f4 start 500
f4 end 600
f1 end 1000
expected Output of the program
f1 600
f2 200
f3 100
f4 100
*/
const logFile = `f1 start 0
f2 start 100
f3 start 200
f3 end 300
f2 end 400
f4 start 500
f4 end 600
f1 end 1000`
const logRows = logFile.split("\n")
// f is the first char of the line so just slice it off
const embedLevel = logRows.map(r => r.slice(1))
// then split the space delimited data for each row
const data = embedLevel.map(r => r.split(" "))
// reverse sort to put the deepest embedded function level first
const sortedData = data.sort().reverse()
let results = []
let lastFuncTime = 0
console.log("sorted data", sortedData)
for (let i = 0; i < sortedData.length - 1; i = i + 2) {
let startRow = sortedData[i]
let endRow = sortedData[i + 1]
let endTime = Number.parseInt(endRow[2], 10)
let startTime = Number.parseInt(startRow[2], 10)
const rawTime = endTime - startTime
const timeWithoutLastFunction = rawTime - lastFuncTime
lastFuncTime = rawTime
// results.push(["f" + startRow[0], timeWithoutLastFunction])
results.push({
f: startRow[0],
rawTime: rawTime,
timeWithoutLastFunction: timeWithoutLastFunction
})
}
console.log("functionNumber rawTime timeWithoutLastFunction")
results.forEach(r =>
console.log("f" + r.f + " " + r.rawTime + " " + r.timeWithoutLastFunction)
)
</script>
<script id="jsbin-source-javascript" type="text/javascript">/* develop a program to calculate the time taken by each function in a performance log file
the format of the log file is given below
f1 start 0
f2 start 100
f3 start 200
f3 end 300
f2 end 400
f4 start 500
f4 end 600
f1 end 1000
expected Output of the program
f1 600
f2 200
f3 100
f4 100
*/
const logFile = `f1 start 0
f2 start 100
f3 start 200
f3 end 300
f2 end 400
f4 start 500
f4 end 600
f1 end 1000`
const logRows = logFile.split("\n")
// f is the first char of the line so just slice it off
const embedLevel = logRows.map(r => r.slice(1))
// then split the space delimited data for each row
const data = embedLevel.map(r => r.split(" "))
// reverse sort to put the deepest embedded function level first
const sortedData = data.sort().reverse()
let results = []
let lastFuncTime = 0
console.log("sorted data", sortedData)
for (let i = 0; i < sortedData.length - 1; i = i + 2) {
let startRow = sortedData[i]
let endRow = sortedData[i + 1]
let endTime = Number.parseInt(endRow[2], 10)
let startTime = Number.parseInt(startRow[2], 10)
const rawTime = endTime - startTime
const timeWithoutLastFunction = rawTime - lastFuncTime
lastFuncTime = rawTime
// results.push(["f" + startRow[0], timeWithoutLastFunction])
results.push({
f: startRow[0],
rawTime: rawTime,
timeWithoutLastFunction: timeWithoutLastFunction
})
}
console.log("functionNumber rawTime timeWithoutLastFunction")
results.forEach(r =>
console.log("f" + r.f + " " + r.rawTime + " " + r.timeWithoutLastFunction)
)
</script></body>
</html>
/* develop a program to calculate the time taken by each function in a performance log file
the format of the log file is given below
f1 start 0
f2 start 100
f3 start 200
f3 end 300
f2 end 400
f4 start 500
f4 end 600
f1 end 1000
expected Output of the program
f1 600
f2 200
f3 100
f4 100
*/
const logFile = `f1 start 0
f2 start 100
f3 start 200
f3 end 300
f2 end 400
f4 start 500
f4 end 600
f1 end 1000`
const logRows = logFile.split("\n")
// f is the first char of the line so just slice it off
const embedLevel = logRows.map(r => r.slice(1))
// then split the space delimited data for each row
const data = embedLevel.map(r => r.split(" "))
// reverse sort to put the deepest embedded function level first
const sortedData = data.sort().reverse()
let results = []
let lastFuncTime = 0
console.log("sorted data", sortedData)
for (let i = 0; i < sortedData.length - 1; i = i + 2) {
let startRow = sortedData[i]
let endRow = sortedData[i + 1]
let endTime = Number.parseInt(endRow[2], 10)
let startTime = Number.parseInt(startRow[2], 10)
const rawTime = endTime - startTime
const timeWithoutLastFunction = rawTime - lastFuncTime
lastFuncTime = rawTime
// results.push(["f" + startRow[0], timeWithoutLastFunction])
results.push({
f: startRow[0],
rawTime: rawTime,
timeWithoutLastFunction: timeWithoutLastFunction
})
}
console.log("functionNumber rawTime timeWithoutLastFunction")
results.forEach(r =>
console.log("f" + r.f + " " + r.rawTime + " " + r.timeWithoutLastFunction)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment