Adding profiling to ZSH

Add this line as the first one, to the very top of your ~/.zshrc:

zmodload zsh/zprof

And add this one to the very bottom of this same file:

zprof

Now, whenever you open up ZSH, you shall see a list of things which start and how much time each entry takes to load. For me it was like this:

num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1        1123.49  1123.49   87.59%   1123.49  1123.49   87.59%  _pyenv-from-homebrew-installed
 2)    1          84.02    84.02    6.55%     59.50    59.50    4.64%  compinit
 3)    3          26.94     8.98    2.10%     26.94     8.98    2.10%  __sdkman_export_candidate_home
 4)    3          25.77     8.59    2.01%     25.77     8.59    2.01%  __sdkman_prepend_candidate_to_path
 5)    2          24.52    12.26    1.91%     24.52    12.26    1.91%  compaudit
 6)    2           7.98     3.99    0.62%      7.98     3.99    0.62%  grep-flag-available
 7)    2           7.54     3.77    0.59%      7.54     3.77    0.59%  env_default
 8)    2           2.43     1.22    0.19%      2.43     1.22    0.19%  _has
 9)   23           2.43     0.11    0.19%      2.43     0.11    0.19%  compdef
10)    1           1.09     1.09    0.09%      1.09     1.09    0.09%  colors
11)   12           0.38     0.03    0.03%      0.38     0.03    0.03%  is_plugin
12)    1           0.38     0.38    0.03%      0.38     0.38    0.03%  is-at-least
13)    1           0.21     0.21    0.02%      0.21     0.21    0.02%  _homebrew-installed
14)    1           0.03     0.03    0.00%      0.03     0.03    0.00%  __sdkman_echo_debug

-----------------------------------------------------------------------------------

I was able to measure the total startup time by running time zsh -i -c exit. And the total startup time was almost two seconds.

zsh -i -c exit  1.16s user 0.73s system 100% cpu 1.889 total

As you can see, pyenv takes whole bunch of time (1123 millis!). So I do have the pyenv plugin for ZSH and I did disable it. Here’s what happened afterwards:

num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1          91.42    91.42   53.38%     62.48    62.48   36.48%  compinit
 2)    2          28.94    14.47   16.90%     28.94    14.47   16.90%  compaudit
 3)    3          28.62     9.54   16.71%     28.62     9.54   16.71%  __sdkman_export_candidate_home
 4)    3          27.65     9.22   16.14%     27.65     9.22   16.14%  __sdkman_prepend_candidate_to_path
 5)    2           8.27     4.14    4.83%      8.27     4.14    4.83%  grep-flag-available
 6)    2           8.22     4.11    4.80%      8.22     4.11    4.80%  env_default
 7)    2           2.55     1.28    1.49%      2.55     1.28    1.49%  _has
 8)   23           2.50     0.11    1.46%      2.50     0.11    1.46%  compdef
 9)    1           1.24     1.24    0.72%      1.24     1.24    0.72%  colors
10)    1           0.41     0.41    0.24%      0.41     0.41    0.24%  is-at-least
11)   10           0.37     0.04    0.21%      0.37     0.04    0.21%  is_plugin
12)    1           0.02     0.02    0.01%      0.02     0.02    0.01%  __sdkman_echo_debug

-----------------------------------------------------------------------------------
zsh -i -c exit  0.28s user 0.23s system 96% cpu 0.526 total

NVM

Not to forget I had NVM installed with the default settings.

With it I had the startup time of 1.9 seconds:

zsh -i -c exit  1.17s user 0.74s system 99% cpu 1.916 total

If you follow this simple instruction or just replace these two lines, initializing NVM in your ~/.zshrc file:

[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

with these lines:

if [ -s "$HOME/.nvm/nvm.sh" ] && [ ! "$(type __init_nvm)" = function ]; then
 export NVM_DIR="$HOME/.nvm"
 [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
 declare -a __node_commands=('nvm' 'node' 'npm' 'yarn' 'gulp' 'grunt' 'webpack')
 function __init_nvm() {
   for i in "${__node_commands[@]}"; do unalias $i; done
   . "$NVM_DIR"/nvm.sh
   unset __node_commands
   unset -f __init_nvm
 }
 for i in "${__node_commands[@]}"; do alias $i='__init_nvm && '$i; done
fi

The start-up time drops by 0.1 second. But it still is an improvement, right?

This post is based on a talk on IntelliJ productivity and contains some tips and tricks I found handy in my everyday work.

You may disagree with me or find these recommendations useless for yourself, but I and some of my teammates have found out these tips have made us more efficient when dealing with lots of code in a big project.

  • say “no!” to tabs – that’s it – disable them. Forever. IntellJ has many ways to navigate the project, just check them out: Cmd+E, Cmd+Alt+← and Cmd+Alt+→

  • stop using mouse – just as with navigation in open files, IntelliJ offers awesome code navigation: want to go to the method definition? Cmd+B, want to find a file or class or method? Shift, Shift or Ctrl+Shift+N or Ctrl+N, want to move around in Project Manager? Cmd+1 (and then Esc to switch between editor and Manager)

  • use IntelliJ’s code intelligence – if you forgot what params method takes, use Cmp+P, if you want to find variable or class or method usage – use Alt+F7, use the power of “Refactor -> Extract Constant/Field” function

  • use action finder popup – that one can save you lot of time when you do not remember where the function needed is located or what key shortcut to use – Cmd+Shift+A is your friend

WARNING: lots of video/gifs under the cut!

Mar 27, 2015

Tiny java code...





tinyjavacode:

This one was super easy so I did a quick comparison of a couple of ways you can do it; the goal is to count all the alphanumeric (a-zA-Z0-9) characters in the text of a Sherlock Holmes book. There’s probably other ways do do it because there’s lots of ways to do literally anything, but the fastest one here is stripping out all non-alphanumeric characters (\W).

Mar 25, 2015

The new hardcore


Trying some new hardcore =3

Just found some interesting way of implementing Decorator design pattern in Python.

As Jason Smith said in his book (Elemental Design Patterns), “design patterns may be implemened in different ways in different programming languages”.

That’s said, design patterns are not some set of classes which will be implemented in a very similar way in different languages - they are just a way of doing something.

Thus, Decorator pattern is a way of wrapping some method’s or class’ behaviour. In Python it may be done with Context Managers:

from contextlib import contextmanager

@contextmanager
def tag(name):
    print "<%s>" % name,
    yield
    print "</%s>" % name,

with tag("h1"):
    print "moo",

print

with tag("div"):
    print "foo",

This code will end up wrapping print "foo" and print "moo" methods with printing some HTML tags around ‘em:

<h1>moo</h1>
<div>foo</div>

That is interesting as it implements Decorator design pattern in a bit hard-coded way, but using language features, not OOP ones.

Compare it to the "standard" OOP implementation in Python:

# -*-coding:utf-8 -*-
class SimpleText(object):
    def __init__(self, text):
        self.text = text

    def content(self):
        return self.text

    def __str__(self):
        return self.content()

class TagDecorator(SimpleText):
    def __init__(self, text, tag):
        super(TagDecorator, self).__init__(text)
        self.tag = tag

    def content(self):
        return '<{0}>{1}</{0}>'.format(self.tag, super(TagDecorator, self).content())

a = SimpleText('moo')
print('SimpleText: %s' % a)

b = TagDecorator('moo', 'h1')
print('TagDecorator (h1): %s' % b)

This one looks a bit… ugly… right? And though Python does not really care of which type are a and b, we may not need all this class hierarchy.

This is the might of context managers!

Have you ever thought on how this:

could be turned to this:

without manually setting per-pixel sizes?

It’s simple as 1-2! Just take a look at the top of the UI editor and you’ll find a set of buttons, dedicated to do this adjustment for you:

Yet, there is just one small tip: first you should select a containing widget, whose children will be rearranged.

Like this:

Note the selected parent widget for vertical_layout.

How did I came over from that to this…
Reworked materials for my rifle
Creating some gun from a rough primitive model with some texture and sculpting. Yet, trying to get some pretty materials on this…
Have had some free time and tried sculpting in Blender =) With some other nice stuff =)