LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2012, 12:22 PM   #16
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301

Maybe writing them in C would be better ? I mean performance would be better.
 
Old 09-19-2012, 04:50 PM   #17
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
@grail,

Your script is very nice. Unfortunately it doesn’t work as a wminfo plugin. The wminfo program uses bash to run the plugins and bash doesn’t understand the Ruby commands.

@ntubski,

Your script is really impressing. I was sure it’s possible to code the binary clock using AWK (gawk) in a more elegant way but I didn’t suppose the code could be so compact.

***

The last version of my script using the better method to get the time-related variables is here:

Code:
#!/bin/bash

dectobin() {
    for i in $*
    do
    echo "$i" | perl -e "printf(\"%04b\n\", <STDIN>)"
    done
}

rotate1()
{
    for (( i=0 ; i<${#1} ; i++ )); do
        printf '%s%s%s%s%s%s\n' "${1:i:1}" "${2:i:1}" "${3:i:1}" "${4:i:1}" "${5:i:1}" "${6:i:1}"
    done
}

string="$(date +" Hlr=%H Mlr=%M Slr=%S")"

$(echo "$string" | sed -E "s/ / export /g;s/lr/l/g;s/=(.)./=\1/g")
$(echo "$string" | sed -E "s/ / export /g;s/lr/r/g;s/=.(.)/=\1/g")

echo "HH MM SS"

rotate1 $(dectobin $(echo -e "$Hl\n$Hr\n$Ml\n$Mr\n$Sl\n$Sr")) | sed -E "s/0/ /g;s/1/*/g;s/(..)(..)(..)/\1|\2|\3|/"
The above script uses the rotate1 function by David the H.

The CPU usage for the above script using the rotate0 function that I found in Internet is 4.1% – 4.7% (median 4.3%).

The CPU usage for the above script using the rotate2 function by danielbmartin is 4.2% – 4.5% (median 4.3%).

The CPU usage for the above script using the rotate3 function by PTrenholme is 4.0% – 4.4% (median 4.0%).

The CPU usage for the above script using the rotate1 function by David the H. is 3.7% – 4.3% (median 4.0%).

The CPU usage for the script by ntubski from post #15 is 1.8% – 2.0% (median 2.0%).

***

Your solution is really great, ntubski. Thank you very much. I could learn AWK for a long time but I never reach you mastery.
 
Old 09-19-2012, 07:47 PM   #18
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by w1k0 View Post
The wminfo program uses bash to run the plugins and bash doesn’t understand the Ruby commands.
wminfo's man page claims:
Quote:
Note that the plugins need not have the extension wmi and need not to be
the shell scripts.
At least you could call the ruby interpreter from bash, something like:
Code:
#!/bin/sh
ruby <<'EOF'
cnt = 0
array = Array.new(6) { Array.new(4) }
...
end
EOF
 
1 members found this post helpful.
Old 09-19-2012, 10:03 PM   #19
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
@ntubski,

The sentence from man wminfo you quoted is one of a few sentences which I borrowed from the documentation written by Robert Kling – the author of the wminfo 1.51. In fact I have no idea what he had in mind writing that so it seems it’s the time to remove that statement from the documentation.

Thank you very much for the tricky method that allows to run different scripts with wminfo.

@grail,

I tested the Ruby script using ntubski’s method. The CPU usage for your script from post #14 is 2.2% – 2.4 (median 2.3%). So its performance is slightly worse than the performance of ntubski’s script from post #15 though still impressing.

@all of you,

It was very interesting and instructing discussion. Thank you very much for your participation once again.
 
Old 09-20-2012, 12:27 AM   #20
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by H_TeXMeX_H
Maybe writing them in C would be better ? I mean performance would be better.
As a plugin for wminfo it wouldn't be better because that program doesn't cope with the plugins other than the shell scripts. (See also ntubski's trick which allows to run some other scripts as well.)
 
Old 09-20-2012, 03:50 AM   #21
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Not sure if it will improve usage or speed but you could also try it as a single call to Ruby:
Code:
ruby -e 'cnt = 0;array = Array.new(6) { Array.new(4) };t = Time.now.strftime("%H%M%S");puts "HH MM SS"; t.each_char{ |c| 0.upto(3){ |n|	array[cnt][n] = c.to_i[n] }; array[cnt].reverse!; cnt+=1 }; transpose_array = array.transpose; transpose_array.each{ |x| x.map!{ |y| y == 0 ? ' ' : '*' } }; transpose_array.each{ |x| 6.step(2, -2) { |n| x.insert(n, '|') }; print "#{x.join}\n" }'
 
Old 09-20-2012, 07:03 AM   #22
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by w1k0 View Post
The sentence from man wminfo you quoted is one of a few sentences which I borrowed from the documentation written by Robert Kling – the author of the wminfo 1.51. In fact I have no idea what he had in mind writing that so it seems it’s the time to remove that statement from the documentation.
I guess that sentence is aspirational rather than descriptive. I don't think it would be that hard to implement though. I gather that you're the current maintainer of wminfo, yes? Would you accept some patches?

Quote:
Originally Posted by w1k0
Quote:
Originally Posted by H_TeXMeX_H
Maybe writing them in C would be better ? I mean performance would be better.
As a plugin for wminfo it wouldn't be better because that program doesn't cope with the plugins other than the shell scripts.
I expect translating my solution to C would give better performance even with the penalty of a bash wrapper script. Also, I don't think measuring %CPU usage is very accurate, the earlier method of timing multiple runs seems like it would give more reliable results.
 
1 members found this post helpful.
Old 09-20-2012, 08:54 AM   #23
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Well ntubski inspired me to do a more straightforward solution too
Code:
ruby -e 'puts "HH MM SS";3.downto(0){ |x| Time.now.strftime("%H|%M|%S|").each_char{ |c| print c == "|"?c:(c.to_i[x]==0?" ":"*")}; puts }'
 
1 members found this post helpful.
Old 09-20-2012, 03:44 PM   #24
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
@grail,

I tested your script from post #23. The CPU usage for it is 2.2% – 2.4% (median 2.3%). So it’s exactly the same as the CPU usage of the first version. As for the script from post #21 I couldn’t run it with wminfo but I don’t expect that the results for it will be different.

@ntubski,

In post #5 I reported the results of the 25,000 runs of two different rotate functions alone. The first function worked 1m5.928s and used 45.5% of CPU – the second function worked 3m27.683s and used 41.0% of CPU.

In post #17 I reported the results for the complete plugins ran by wminfo. The plugin using the first function used 4.0% of CPU and the plugin using the second function used 4.3% of CPU.

Now I tested these two plugins running 1500 iterations and sending the output to /dev/null. The first script worked 1m51.615s and used 39.9% of CPU – the second script worked 1m52.213s and used 43.0% of CPU.

As you can see these last values of the CPU usage are ten times higher than the results for the real CPU usage from post #17. So both these methods of testing the wminfo plugins are equally good.

But the result such as 39.9% can only scary the user. The result such as 4.0% tells the user how high is the real CPU usage of both wminfo and the plugin using the first rotate function.

***

I’m the maintainer of wminfo since the version 2.00 released in August, 2011. Robert Kling – the author of the original wmInfo – stopped to develop it releasing the version 1.51 in August, 2000. All the official versions of the program are here.

I’ll be more than happy seeing some patches for wminfo or the plugins as well as the new plugins if you have some interesting ideas. Yesterday I added to wminfo the THANKS file to thank you for your version of the binary clock. Depending on your future participation in that project I’ll add the further thanks to that file or I’ll add you to the list of the authors of the program.

I don’t think the binary plugins are good solution especially started by bash wrapper script though you could try to convince me. Or maybe you could modify the wminfo to run binary plugins using some special switch (for example: -w for wrapper). (Not so good idea so see the post #28 for the better one.)

As for the improvements of wminfo and the plugins I did a lot to reduce the CPU power demands of the program and I prepared different versions of the plugins in order to lower the CPU usage. Taking into consideration your abilities I’m sure you could improve these plugins much more.

Now I prepare wminfo 3.1.0. For your convenience I put the pre-release here: http://linux-bsd-unix.strefa.pl/wminfo-pre3.1.0.tar.gz. I’ll update that file at the end of each day assuming I’ll made some changes in it.

By the way: are you Window Maker user or you just like to code? I use Window Maker exclusively since 1998. I published my desktop here: This is my Slackware desktop... – my current desktop is similar to the third screenshot from the first post. In the past I used a lot of different dockable applications. Now I use wminfo only. It can replace all useful dockable applications providing more information in most cases.

@all of you,

Is it the most active solved thread ever?

Last edited by w1k0; 09-20-2012 at 09:17 PM. Reason: bad idea – good idea
 
Old 09-20-2012, 04:58 PM   #25
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Stung by the "Frankenstein code" comment by H_TexMex_H , here's an "all bash" version:
Code:
#!/bin/bash

date=$(date '+%H|%M|%S')
Hl=${date:0:1}
Hr=${date:1:1}
Ml=${date:3:1}
Mr=${date:4:1}
Sl=${date:6:1}
Sr=${date:7:1}

dectobin() {
  local dec bin i
  for i in $*
  do
    dec=$((${i} + 0))
    bin=""
    while [ ${dec} -gt 0 ]
    do
      digit=$((dec % 2))
      dec=$((dec / 2))
      bin=${digit}${bin}
    done
    bin="0000${bin}"
    bin=${bin: -4}
    printf -v ${i} "%s" ${bin}
  done
}

rotate()
{
  local i j k max
  max=0
  for i in $*
  do
    [ ${max} -lt ${#i} ] && max=${#i}
  done
  for (( i=0 ; i<${max} ; i++ ))
  do
    k=0
    for j in $*
    do
      if [ ${j:${i}:1} == "1" ]
      then
        printf '*'
      else
        printf ' '
      fi
      [ $((k++ % 2)) -eq 1 ] && printf '|'
    done
    printf '\n'
  done
}

dectobin Hl Hr Ml Mr Sl Sr

echo "${date}"
echo "HH|MM|SS"

rotate $Hl $Hr $Ml $Mr $Sl $Sr
Producing this:
Code:
$ ./rotate2.sh 
14|44|43
HH|MM|SS
  |  |  |
 *|**|* |
  |  | *|
* |  | *|
 *|**|* |
  |  | *|
* |  | *|
(Note that I combined the output and rotate in one function. I seemed to me that printing the rotated binary values and then using perl to format and print them again was, um, somewhat Frankenstein-ish.

Last edited by PTrenholme; 09-20-2012 at 05:00 PM.
 
1 members found this post helpful.
Old 09-20-2012, 05:40 PM   #26
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
@PTrenholme,

H_TexMex_H addressed the “Frankenstein code” comment to you but in fact that code was in part mine and in part yours so we made that monster together. And my original script from post #7 was “Frankensteinish” as well assuming that adjective means a combination of different elements (or languages).

I’ll test the performance of your script tomorrow though I don’t expect the spectacular effects in the comparison to ntubski’s script from post #15.
 
Old 09-20-2012, 08:07 PM   #27
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
@PTrenholme,

I was too curious to wait a few hours for the results so I tested your new script now. It uses from 2.0% to 2.0% of CPU (the median is 2.0%). Taking into consideration the median your script has the same performance as the ntubski’s script. It’s also very stable because it uses the same percentage of the CPU power all the time. Your method to get the time-related variables is better than my new method from the script #17.

I like your new script very much so I’ll attach it to the next release of wminfo together with the ntubski’s script.
 
Old 09-20-2012, 09:01 PM   #28
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
@ntubski,

After reconsidering the problem which I discussed in post #24 I think that instead of the special switch for the wrapper the better idea is to learn wminfo to distinguish the type of the plugin on the basis of the extension: .wmi or .sh is a shell script, .bin is a binary file, .awk is an AWK script, .pl is a Perl script, .rb is a Ruby script etc.

I assume you could modify wminfo to execute binaries as well as AWK, Perl, and Ruby scripts without the shell wrapper script.

Last edited by w1k0; 09-20-2012 at 09:30 PM. Reason: assumption
 
Old 09-21-2012, 12:13 AM   #29
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Well it would appear I am not currently knowledgeable enough about Ruby to get any improvement in this situation ... but I think I can improve on the bash:
Code:
#!/bin/bash

current_time=$(date '+%H|%M|%S|')

echo $current_time
echo "HH MM SS"

for (( bin = 8; bin >= 1; bin/=2 ))
do
    for (( cnt=0; cnt < ${#current_time}; cnt++ ))
    do
        num=${current_time:cnt:1}

        if [[ $num == '|' ]]
        then
            echo -n $num
            continue
        fi

        if (( num & bin ))
        then
            echo -n '*'
        else
            echo -n ' '
        fi

    done
    echo
done
 
1 members found this post helpful.
Old 09-21-2012, 10:00 AM   #30
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Way back in post #5 of this thread OP said:
Code:
Now I have the next question.  The output of the script is:
011
110
101
100
but I need to present the data in the other way. 
Instead of “0” I need “A” and instead of “1” I need “B”.
I tried to do this by modifying my awk several ways, without success.
This is an example of what didn't work.
Code:
VAR1="0111"
VAR2="1100"
VAR3="1010"
echo -e "$VAR1\n$VAR2\n$VAR3"  \
|awk -F "" '{for (rownum=1; rownum<=NF; i++) \
  a[rownum]=a[rownum]substr("AB",1+$i,1)}  \
  END {i=1; while (i in a) {print a[i]; i++;}}'
Please, expert awkers, show how this ought to be done.
Thank you.

Daniel B. Martin
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert columns of data into rows jaufer Linux - Software 3 06-29-2010 05:19 PM
convert columns to rows (tab separated file to csv) doug23 Programming 16 08-16-2009 09:14 PM
Script to convert logs columns to rows fono Linux - Software 10 05-19-2009 08:29 PM
How to print data in rows and columns suran Linux - General 3 03-15-2009 02:53 PM
text data conversion: rows into columns frankie_DJ Programming 6 06-03-2006 06:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration