Skip to content

Instantly share code, notes, and snippets.

@tankala
tankala / ExecutionTimeMeasurementExample.java
Created April 21, 2018 09:48
Execution Time Measurement Example in Java
public class ExecutionTimeMeasurementExample {
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis();
sleepFunction(1000);
sleepFunction(1000);
long endTime = System.currentTimeMillis();
System.out.println("Program took " + (endTime - startTime) + " ms to complete");
}
private static void sleepFunction(int milliSecondsToSleep) throws InterruptedException {
@tankala
tankala / WrongWayToMeasureExecutionTime.js
Created April 21, 2018 10:00
Wrong way to measure execution time in Node.js
var sleepFunction = function(milliSecondsToSleep) {
let startTime = new Date().getTime();
setTimeout(function() {
console.log("Dummy CallBack");
}, milliSecondsToSleep);
let endTime = new Date().getTime();
console.log("My job done in " + (endTime - startTime) + " ms");
}
let startTime = new Date().getTime();
@tankala
tankala / CoolWayToMeasureExecutionTime.js
Created April 21, 2018 10:25
Cool way to measure execution time in Node.js
var sleepFunction = function(milliSecondsToSleep, timerLabel) {
console.time(timerLabel);
setTimeout(function() {
console.timeEnd(timerLabel);
}, milliSecondsToSleep);
}
console.time("Program");
sleepFunction(1000, "FunctionCall1");
sleepFunction(1000, "FunctionCall2");
@tankala
tankala / CallBackWayToMeasureExecutionTime.js
Last active April 21, 2018 10:41
CallBack way to measure execution time in Node.js
var sleepFunction = function(milliSecondsToSleep) {
let startTime = new Date().getTime();
setTimeout(function() {
let endTime = new Date().getTime();
console.log("My job done in " + (endTime - startTime) + " ms");
}, milliSecondsToSleep);
}
let startTime = new Date().getTime();
sleepFunction(1000);
@tankala
tankala / ConsumerGroupInitiation.js
Last active April 24, 2018 15:08
Consumer Group Initiation in Node.js
var kafka = require('kafka-node');
exports.initiateKafkaConsumerGroup = function (groupName, topicName) {
var options = {
// connect directly to kafka broker (instantiates a KafkaClient)
kafkaHost: '127.0.0.1:9092',
groupId: groupName,
autoCommit: true,
autoCommitIntervalMs: 5000,
sessionTimeout: 15000,
@tankala
tankala / KafkaProducer.js
Created April 24, 2018 15:22
Kafka Producer in Node.js
var kafka = require('kafka-node');
var producer = null;
var readyFlag = false;
var BaseModel = function () {
};
BaseModel.prototype.produceJob = function (topic, pload, isBatchProducer, callback) {
module.exports.getProducer(topic, pload, isBatchProducer);
@tankala
tankala / listFilesInFolder.js
Created May 23, 2018 08:02
List all files in a directory in Node.js recursively
//We can use fs.readdir or fs.readdirSync methods
const fs = require('fs');
const testFolder = __dirname + '/files/';
//Asynchronous way
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
});
})
@tankala
tankala / mnistTensorFlowJS.html
Created May 26, 2018 19:38
MNIST Example using TensorFlow.js
<html>
<head>
<title>MNIST</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.2"> </script>
<script type="text/javascript">
// Variables for referencing the canvas and 2dcanvas context
var canvas, ctx;
// Variables to keep track of the mouse position and left-button status
@tankala
tankala / table.html
Created June 9, 2018 11:24
HTML code for populating table with data which is present in json file with name jsonData.json
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function onLoad() {
var url = 'jsonData.json';
$.getJSON(url, function (json) {
var table = $('<table>');
@tankala
tankala / jsonData.json
Created June 9, 2018 11:26
JSON data which can be used to populate table
[
{
"ID": 1,
"Name": "Tankala Ashok",
"IDNumber": "1234"
},
{
"ID": 2,
"Name": "Himabindu",
"IDNumber": "2345"