Video for using Node.js for Everything

This is another talks video by Charlie Key, the CEO at Modulus, he share with us how Node.JS succeed for development nearly every aspects for a startup where load balance, performance, usability, enterprise class are considered.

He introduced and demo some new tools like Node-inspector, which is a web based inspector to inspect/debug Node code. The debugger interface is nearly same as the native Google Chrome/Safari Developer tools which developed for inspect client web JavaScript and this web based inspector is a completely rewrite mimic the look and feel and features of the native inspector just using JavaScript, and quite powerful, and quite interesting.

Meanwhile he introduced Modulus, an enterprise class Node.JS + MongoDB cloud platform, have a look.

Video for JavaScript & Our Obsession with Speed

This is a talks by Brian Lonsdorf, the CTO of Loop/Recur, who at least last 5 years stuck in JavaScript development, give a talks why we should not always comes to performance in an early stage, and he talks about the benefits of declarative over imperative way.

I spent my time to watch this video and I found useful so I share here to you. Now is Singapore Time 1:15am and I watched this video when I reached home from works just now, but unfortunately I slept in front of computer after this video nearly finished… But I should blog this video before sleep.

MIT 最新技术:从静音视频中提取声音

MIT 最新技术:从静音视频中提取声音
太夸张了。
MIT Latest Technology, extract audio from an audio absent video.
Core objective: every sound wave get a very tiny vibration when it reach an object. Analyse the vibration can get back the original sound wave.

Link: http://blog.jobbole.com/74941/

English version: link

Linux Grep and Windows Find

When working I experienced using Linux Grep and Windows Find in terminal to search string. It is quite useful so I share here and for future reference.

When using Windows, we may use

netstat -abon | find "Segments"

When using Linux, we may use

ifconfig | grep "192.168"

When using Linux to search occurrences of something over folders

grep -r "something to search" "folder name"

Trying on Java 8 Stream Parallel

I had been trying on Java 8 Stream Parallel.

Refer following code:

import java.util.*;
public class Test {
	public static void main(String args[]) {
		List list = new ArrayList<>();
		list.add(new Employee("First", "YongHao"));
		list.add(new Employee("Second", "YongHao"));
		list.add(new Employee("Third", "YongHao"));
		
		list.stream().sorted((e1, e2) -> e1.getFirstName().compareTo(e2.getFirstName()))
		        .forEach(System.out::println);
		
		System.out.println("--------------");
		
		list.stream().parallel().sorted(Comparator.comparing(e -> e.getFirstName()))
		        .forEachOrdered(System.out::println);
		        
		        
	}
	
	public static class Employee {
		String firstName;
		String lastName;
		public Employee(String firstName, String lastName) {
			this.firstName = firstName;
			this.lastName = lastName;
		}
		public String getFirstName() {
			return firstName;
		}
		public String getLastName() {
			return lastName;
		}
		public String toString() {
			return firstName + " " + lastName;
		}
	}
}

The result will be:
➜ java2 javac Test.java
➜ java2 java Test
First YongHao
Second YongHao
Third YongHao
————–
First YongHao
Second YongHao
Third YongHao

Look at first:

list.stream().sorted((e1, e2) -> e1.getFirstName().compareTo(e2.getFirstName()))
.forEach(System.out::println);

It first returns the list as a stream, sort the list using the lambda expression and then iterating the list and print its output.

Look at second:

list.stream().parallel().sorted(Comparator.comparing(e -> e.getFirstName()))
.forEachOrdered(System.out::println);

Notice, after returned stream-ed list, there is a chained parallel(), which will actually doing background task distribution fully utilizing your system cores. And notice, forEachOrdered method at the end, which guarantees the ordered result be returned. If we replace forEachOrdered to forEach, you will see random order be returned. Because forEach method cannot guarantee ordered result when doing parallel operation.

Another note, see the chained operation, from the list, to stream(), parallel(), sorted(), at the point Java had not yet execute anything, list.stream() did return a stream-ed list, or more formally, a lazy list, the actual execution started when we call forEach or forEachOrdered function.

I did some simple benchmark to measure the speed for these two, one without parallel and one with parallel.

Here come modified code:

import java.util.*;
public class Test {
	public static void main(String args[]) {
		List list = new ArrayList<>();
		generateList(list);
		long start = System.currentTimeMillis();
		list.stream().sorted((e1, e2) -> e1.getFirstName().compareTo(e2.getFirstName()))
		        .toArray();
		System.out.println("Elapsed:"+(System.currentTimeMillis() - start));
		System.out.println("--------------");
		start = System.currentTimeMillis();
		list.stream().parallel().sorted(Comparator.comparing(e -> e.getFirstName()))
		        .toArray();
		System.out.println("Elapsed:"+(System.currentTimeMillis() - start));        
	}
	
	private static void generateList(List list) {
		for(int i = 0; i < 2000000; i++) {
			Employee e = new Employee("EMP" + i, "YONGHAO");
			list.add(e);
		}
	}
	
	public static class Employee {
		String firstName;
		String lastName;
		public Employee(String firstName, String lastName) {
			this.firstName = firstName;
			this.lastName = lastName;
		}
		public String getFirstName() {
			return firstName;
		}
		public String getLastName() {
			return lastName;
		}
		public String toString() {
			return firstName + " " + lastName;
		}
	}
}

The result will be:
java2 javac Test.java
➜ java2 java Test
Elapsed:1901
--------------
Elapsed:337
➜ java2 java Test
Elapsed:296
--------------
Elapsed:494
➜ java2
➜ java2 java Test
Elapsed:288
--------------
Elapsed:1425
➜ java2
➜ java2 java Test
Elapsed:1834
--------------
Elapsed:422

OK, the result showed that... well, most of the time parallel operation faster, actually it should be faster when operating quite big data. Because it distributes the tasks to multiple cores.

OK, conclusion, worth to learn this new technology.

Stream and Lambda is the new features added in Java 8. A lot more to explore.