No torrent trackers out there anymore?

uTorrent — got crazy, starting to use client as bitcoin miner

qBittorrent – does “checking” operation 10 times slower, than uTorrent

Transmission – doesn’t understand some *.torrent files, that uTorrent understands

Deluge – can’t write into some locations, where uTorrent can write

We can use latitude to convert coordinates into meters

I met Yelp Fusion API, which can find objects withing given radius around given location on Earth. Radius should be given in meters.

Simultaneously, Google Maps API uses degrees of latitude and longitude to report map bounds.

How to convert one to another so that provide meters to Yelp API? I realized, that this is pretty easy if we use latitude only:

EarthLatitude

Where alpha is latitude difference in radians.

To convert to degrees we should

2 * Pi / 360. *6371008.8 = 111195 meters per degree of latitude.

I wrote as simple JSFiddle here to illustrate this approach: https://jsfiddle.net/dimskraft/1boks8es/2/

Here is screenshot:

MetersPerDegree screenshot

Swing IOC

I wrote simple project to demonstrate how to create (desktop) UI with IOC. The structure of JForm is defined by special Spring configuration, which contains beans, named by convention. Dedicated service takes configuration, creates child context and set up window according to it. Having child context both isolates it’s beans from overall application beans and also can help to simulate web scopes if each such context will be associated with JFrame.

Also I tried to use RxSwing to bing list to an image.

SwingIOC_Example

https://github.com/dims12/SwingIOC

 

Running Tensorflow train with GPU

Was not realizing, that training of DNN with GPU is so faster, than with CPU.

I have old GTX770 card which is reported to have 3,213 GFLOPS.

My main CPU is Core i7-4770K 3.5 GHz, which is reported to have 177 GFLOPS.

It looks like my GPU should be 18 times faster than CPU.

Actually, I don’t observe this kind of speed. I wrote simple convolutional model for CIFAR-10 database and observed that is works approximately 7 times faster on GPU than on CPU (by eye).

Fastest way to generate random BufferedImage (in Java)

Today I decided to generate random image for an image stub, like this:

fillrandom01

All RGB values are uniformly random here.

How to generate such image in Java?

I wrote a simple program to test:

package com.inthemoon.snippets.swing;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.VolatileImage;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.util.Random;

/**
 * Created by Dims on 28.02.2017.
 */
public class FastRandomImageGenerator {

   private final static int width = 1024;
   private final static int height = 768;
   private final static int count = 1000;

   private final static Random rnd = new Random();

   private static int[] array;


   public static void main(String[] args) throws IOException {

      long start, end;
      double elapsed;
      long fps;

      BufferedImage image;
      image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
      array = new int[image.getWidth() * image.getHeight() * image.getRaster().getNumBands()];


      start = System.currentTimeMillis();
      for(int i=0; i<count; ++i) {
         fillRandom00(image);
      }
      end = System.currentTimeMillis();

      elapsed = (double)(end - start) / count;
      fps = Math.round(1000. / elapsed);

      System.out.println(String.format(Locale.US, "Elapsed: %.2f milliseconds per one fillRandom00, expected fps is %d", elapsed, fps));
      //ImageIO.write(image, "PNG", new File("fillRandom01.png"));




      start = System.currentTimeMillis();
      for(int i=0; i<count; ++i) {
         fillRandom01(image);
      }
      end = System.currentTimeMillis();

      elapsed = (double)(end - start) / count;
      fps = Math.round(1000. / elapsed);

      System.out.println(String.format(Locale.US, "Elapsed: %.2f milliseconds per one fillRandom01, expected fps is %d", elapsed, fps));
      ImageIO.write(image, "PNG", new File("fillRandom01.png"));



      start = System.currentTimeMillis();
      for(int i=0; i<count; ++i) {
         fillRandom02(image);
      }
      end = System.currentTimeMillis();

      elapsed = (double)(end - start) / count;
      fps = Math.round(1000. / elapsed);

      System.out.println(String.format(Locale.US, "Elapsed: %.2f milliseconds per one fillRandom02, expected fps is %d", elapsed, fps));
      ImageIO.write(image, "PNG", new File("fillRandom02.png"));




      start = System.currentTimeMillis();
      for(int i=0; i<count; ++i) {
         fillRandom03(image);
      }
      end = System.currentTimeMillis();

      elapsed = (double)(end - start) / count;
      fps = Math.round(1000. / elapsed);

      System.out.println(String.format(Locale.US, "Elapsed: %.2f milliseconds per one fillRandom03, expected fps is %d", elapsed, fps));
      ImageIO.write(image, "PNG", new File("fillRandom03.png"));


   }


   public static void fillRandom00(BufferedImage image) {
      for(int x=0; x<image.getWidth(); ++x) {
         for(int y=0; y<image.getHeight(); ++y) {
            rnd.nextInt(0xFFFFFF);
         }
      }
   }


   public static void fillRandom01(BufferedImage image) {
      for(int x=0; x<image.getWidth(); ++x) {
         for(int y=0; y<image.getHeight(); ++y) {
            image.setRGB(x, y, rnd.nextInt(0xFFFFFF));
         }
      }
   }


   public static void fillRandom02(BufferedImage image) {
      for(int i=0; i<array.length; ++i) {
         array[i] = rnd.nextInt(0xFF);
      }
      image.getRaster().setPixels(0, 0, image.getWidth(), image.getHeight(), array);
   }


   public static void fillRandom03(BufferedImage image) {
      int[] array = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
      for(int i=0; i<array.length; ++i) {
         array[i] = rnd.nextInt(0xFFFFFF);
      }
   }

}

“fillRandom00” method is just generating WxH number of random values.

“fillRandom01” is using straightforward method utilizing BufferedImage#setRGB()

“fillRandom02” is trying to prepare array of ints separately and bitblt them onto BufferedImage.

And the latest method “fillRandom03” is using direct access to BuffereImage data.

Output of the program is following:

Elapsed: 7.97 milliseconds per one fillRandom00, expected fps is 125
Elapsed: 32.01 milliseconds per one fillRandom01, expected fps is 31
Elapsed: 28.75 milliseconds per one fillRandom02, expected fps is 35
Elapsed: 7.90 milliseconds per one fillRandom03, expected fps is 127

As it can be expected, the latest method appeared as fastest. But surprise is that it appeared even faster, than just generating numbers. Probably, this is effect of nested loop?