http://hissa.nist.gov/HHRFdata/Artifacts/ITLdoc/235/sttoc.htm
http://checkstyle.sourceforge.net/index.html
Monday, April 20, 2009
Testing with complexity bookmarks:
Posted by
Roman G.
at
11:51 PM
0
comments
Labels: concepts, Language comparison, reading
Sunday, April 19, 2009
Code reviews and luck of requirements
Code reviews with absence of requirements can not provide quality of software themself.
consider example of change:
main()
{
...
--- return ret;
+++ return 0;
}
If this code is ok?
It depends only from requirements.
The code is ok, if the code satisfied requirements, and not ok, if not.
Syntactic correctness actually tells nothing.
Posted by
Roman G.
at
11:04 PM
0
comments
Labels: analysis, concepts, Language comparison
Sunday, December 7, 2008
My Ubuntu installation config variables:
Linux kubic-roman 2.6.24-22-generic #1 SMP Mon Nov 24 18:32:42 UTC 2008 i686 GNU/Linux
generated by APUE2 book chapter 2 program:
opt.txt
_POSIX_ADVISORY_INFO is defined (val is 200112)
sysconf says _POSIX_ADVISORY_INFO = 200112
_POSIX_ASYNCHRONOUS_IO is defined (val is 200112)
sysconf says _POSIX_ASYNCHRONOUS_IO = 200112
_POSIX_BARRIERS is defined (val is 200112)
sysconf says _POSIX_BARRIERS = 200112
_POSIX_CPUTIME is defined (val is 0)
sysconf says _POSIX_CPUTIME = 200112
_POSIX_CLOCK_SELECTION is defined (val is 200112)
sysconf says _POSIX_CLOCK_SELECTION = 200112
_POSIX_FSYNC is defined (val is 200112)
sysconf says _POSIX_FSYNC = 200112
_POSIX_IPV6 is defined (val is 200112)
sysconf says _POSIX_IPV6 = 200112
_POSIX_MAPPED_FILES is defined (val is 200112)
sysconf says _POSIX_MAPPED_FILES = 200112
_POSIX_MEMLOCK is defined (val is 200112)
sysconf says _POSIX_MEMLOCK = 200112
_POSIX_MEMLOCK_RANGE is defined (val is 200112)
sysconf says _POSIX_MEMLOCK_RANGE = 200112
_POSIX_MONOTONIC_CLOCK is defined (val is 0)
sysconf says _POSIX_MONOTONIC_CLOCK = 200112
_POSIX_MEMORY_PROTECTION is defined (val is 200112)
sysconf says _POSIX_MEMORY_PROTECTION = 200112
_POSIX_MESSAGE_PASSING is defined (val is 200112)
sysconf says _POSIX_MESSAGE_PASSING = 200112
_POSIX_PRIORITIZED_IO is defined (val is 200112)
sysconf says _POSIX_PRIORITIZED_IO = 200112
_POSIX_PRIORITIZED_SCHEDULING is undefined
no symbol for _POSIX_PRIORITIZED_SCHEDULING
_POSIX_RAW_SOCKETS is defined (val is 200112)
sysconf says _POSIX_RAW_SOCKETS = 200112
_POSIX_REALTIME_SIGNALS is defined (val is 200112)
sysconf says _POSIX_REALTIME_SIGNALS = 200112
_POSIX_SEMAPHORES is defined (val is 200112)
sysconf says _POSIX_SEMAPHORES = 200112
_POSIX_SHARED_MEMORY_OBJECTS is defined (val is 200112)
sysconf says _POSIX_SHARED_MEMORY_OBJECTS = 200112
_POSIX_SYNCHRONIZED_IO is defined (val is 200112)
sysconf says _POSIX_SYNCHRONIZED_IO = 200112
_POSIX_SPIN_LOCKS is defined (val is 200112)
sysconf says _POSIX_SPIN_LOCKS = 200112
_POSIX_SPAWN is defined (val is 200112)
sysconf says _POSIX_SPAWN = 200112
_POSIX_SPORADIC_SERVER is defined (val is -1)
sysconf says _POSIX_SPORADIC_SERVER = (no limit)
_POSIX_THREAD_CPUTIME is defined (val is 0)
sysconf says _POSIX_THREAD_CPUTIME = 200112
_POSIX_TRACE_EVENT_FILTER is defined (val is -1)
sysconf says _POSIX_TRACE_EVENT_FILTER = (no limit)
_POSIX_TIMEOUTS is defined (val is 200112)
sysconf says _POSIX_TIMEOUTS = 200112
_POSIX_TIMERS is defined (val is 200112)
sysconf says _POSIX_TIMERS = 200112
_POSIX_THREAD_PRIO_INHERIT is defined (val is 200112)
sysconf says _POSIX_THREAD_PRIO_INHERIT = 200112
_POSIX_THREAD_PRIO_PROTECT is defined (val is 200112)
sysconf says _POSIX_THREAD_PRIO_PROTECT = 200112
_POSIX_THREAD_PRIORITY_SCHEDULING is defined (val is 200112)
sysconf says _POSIX_THREAD_PRIORITY_SCHEDULING = 200112
_POSIX_TRACE is defined (val is -1)
sysconf says _POSIX_TRACE = (no limit)
_POSIX_TRACE_INHERIT is defined (val is -1)
sysconf says _POSIX_TRACE_INHERIT = (no limit)
_POSIX_TRACE_LOG is defined (val is -1)
sysconf says _POSIX_TRACE_LOG = (no limit)
_POSIX_THREAD_ATTR_STACKADDR is defined (val is 200112)
sysconf says _POSIX_THREAD_ATTR_STACKADDR = 200112
_POSIX_THREAD_SAFE_FUNCTIONS is defined (val is 200112)
sysconf says _POSIX_THREAD_SAFE_FUNCTIONS = 200112
_POSIX_THREAD_PROCESS_SHARED is defined (val is 200112)
sysconf says _POSIX_THREAD_PROCESS_SHARED = 200112
_POSIX_THREAD_SPORADIC_SERVER is defined (val is -1)
sysconf says _POSIX_THREAD_SPORADIC_SERVER = (no limit)
_POSIX_THREAD_ATTR_STACKSIZE is defined (val is 200112)
sysconf says _POSIX_THREAD_ATTR_STACKSIZE = 200112
_POSIX_THREADS is defined (val is 200112)
sysconf says _POSIX_THREADS = 200112
_POSIX_TYPED_MEMORY_OBJECTS is defined (val is -1)
sysconf says _POSIX_TYPED_MEMORY_OBJECTS = (no limit)
_XOPEN_UNIX is defined (val is 1)
sysconf says _XOPEN_UNIX = 1
_XOPEN_STREAMS is undefined
no symbol for _XOPEN_STREAMS
_XOPEN_CRYPT is defined (val is 1)
sysconf says _XOPEN_CRYPT = 1
_XOPEN_LEGACY is defined (val is 1)
sysconf says _XOPEN_LEGACY = 1
_XOPEN_REALTIME is defined (val is 1)
sysconf says _XOPEN_REALTIME = 1
_XOPEN_REALTIME_THREADS is defined (val is 1)
sysconf says _XOPEN_REALTIME_THREADS = 1
_POSIX_JOB_CONTROL is defined (val is 1)
sysconf says _POSIX_JOB_CONTROL = 1
_POSIX_READER_WRITER_LOCKS is defined (val is 200112)
sysconf says _POSIX_READER_WRITER_LOCKS = 200112
_POSIX_REGEXP is defined (val is 1)
sysconf says _POSIX_REGEXP = 1
_POSIX_SAVED_IDS is defined (val is 1)
sysconf says _POSIX_SAVED_IDS = 1
_POSIX_SHELL is defined (val is 1)
sysconf says _POSIX_SHELL = 1
_XOPEN_ENH_I18N is defined (val is 1)
sysconf says _XOPEN_ENH_I18N = 1
_XOPEN_SHM is defined (val is 1)
sysconf says _XOPEN_SHM = 1
_POSIX_VERSION is defined (val is 200112)
sysconf says _POSIX_VERSION = 200112
_XOPEN_VERSION is defined (val is 600)
sysconf says _XOPEN_VERSION = 600
_POSIX_CHOWN_RESTRICTED is defined (val is 1)
pathconf says _POSIX_CHOWN_RESTRICTED = 1
_POSIX_NO_TRUNC is defined (val is 1)
pathconf says _POSIX_NO_TRUNC = 1
_POSIX_VDISABLE is defined (val is 0)
pathconf says _POSIX_VDISABLE = 0
POSIX_ASYNC_IO is undefined
pathconf says POSIX_ASYNC_IO = (no limit)
POSIX_PRIO_IO is undefined
pathconf says POSIX_PRIO_IO = (no limit)
POSIX_SYNC_IO is undefined
pathconf says POSIX_SYNC_IO = (no limit)
----%<----
ARG_MAX defined to be 131072
ARG_MAX = 131072
no symbol for ATEXIT_MAX
ATEXIT_MAX = 2147483647
CHARCLASS_NAME_MAX defined to be 2048
CHARCLASS_NAME_MAX = 2048
no symbol for CHILD_MAX
CHILD_MAX = 8125
no symbol for CLOCKTICKSPERSECOND /*clock ticks/second*/
CLOCKTICKSPERSECOND /*clock ticks/second*/ = 100
COLL_WEIGHTS_MAX defined to be 255
COLL_WEIGHTS_MAX = 255
HOST_NAME_MAX defined to be 64
HOST_NAME_MAX = 64
IOV_MAX defined to be 1024
IOV_MAX = 1024
LINE_MAX defined to be 2048
LINE_MAX = 2048
LOGIN_NAME_MAX defined to be 256
LOGIN_NAME_MAX = 256
NGROUPS_MAX defined to be 65536
NGROUPS_MAX = 65536
no symbol for OPEN_MAX
OPEN_MAX = 1024
no symbol for PAGESIZE
PAGESIZE = 4096
no symbol for PAGE_SIZE
PAGE_SIZE = 4096
RE_DUP_MAX defined to be 32767
RE_DUP_MAX = 32767
no symbol for STREAM_MAX
STREAM_MAX = 16
no symbol for SYMLOOP_MAX
SYMLOOP_MAX = (no limit)
TTY_NAME_MAX defined to be 32
TTY_NAME_MAX = 32
no symbol for TZNAME_MAX
TZNAME_MAX = 6
MAX_CANON defined to be 255
MAX_CANON = 255
MAX_INPUT defined to be 255
MAX_INPUT = 255
no symbol for FILESIZEBITS
FILESIZEBITS = 64
no symbol for LINK_MAX
LINK_MAX = 32000
NAME_MAX defined to be 255
NAME_MAX = 255
PATH_MAX defined to be 4096
PATH_MAX = 4096
PIPE_BUF defined to be 4096
PIPE_BUF = 4096
no symbol for SYMLINK_MAX
SYMLINK_MAX = (no limit)
Posted by
Roman G.
at
10:32 PM
0
comments
Labels: analysis, C, GNU/Linux, Language comparison, performance, Ubuntu, Unix
Thursday, August 14, 2008
Cygwin pthreads exit causes access violation example
Example of stack overflow in cygwin.
While researched linking and AIX portability, tried example from there:
http://www.ibm.com/developerworks/aix/library/au-gnu.html?S_TACT=105AGX99&S_CMP=CP
(it is erroneous; simple mistakes were fixed):
After exit on cygwin it warns with access violation.
Executable than blocked (for removal).
Posted by
Roman G.
at
1:13 AM
0
comments
Labels: AIX, C, concepts, GNU/Linux, Language comparison, performance
Tuesday, July 22, 2008
Gcc auto-vectorization
Code: vect_auto.c
#include <stdio.h>
#include <string.h>
int main()
{
int i,j,k,l;
int a[4096];
int b[4096];
memset((void *)a,0,4096);
memset((void *)b,0,4096);
for(i=0;i<4096;i++)
{
a[i] =i%67;
b[i]= i%87;
}
for(k=0;k<200000;k++)
for(i=0;i<4096;i++)
a[i] = a[i]+b[i];
l=0;
for(j=0;j<4096;j++)
{l=l+a[i];}
// printf("%d\n",l);
return l;
}
$ gcc -O3 -ftree-vectorize vect_auto.c -o vect_o
disasm:
0x0000000000400530: movdqa (%rax,%rbp,1),%xmm0
0x0000000000400535: paddd (%rax,%rbx,1),%xmm0
0x000000000040053a: movdqa %xmm0,(%rax,%rbx,1)
0x000000000040053f: add $0x10,%rax
0x0000000000400543: cmp $0x4000,%rax
0x0000000000400549: jne 0x400530
$ gcc -O3 vect_auto.c -o vect_o_no
disasm:
0x0000000000400528: xor %edx,%edx
0x000000000040052a: nopw 0x0(%rax,%rax,1)
0x0000000000400530: mov 0x0(%rbp,%rdx,4),%eax
0x0000000000400534: add %eax,(%rbx,%rdx,4)
0x0000000000400537: add $0x1,%rdx
0x000000000040053b: cmp $0x1000,%rdx
0x0000000000400542: jne 0x400530
0x0000000000400544: add $0x1,%ecx
0x0000000000400547: cmp $0x30d40,%ecx
0x000000000040054d: jne 0x400528
[rtg@rtgCent vect]$ time ./vect_o_no
real 0m2.178s
user 0m2.128s
sys 0m0.003s
[rtg@rtgCent vect]$ time ./vect_o
real 0m0.867s
user 0m0.835s
sys 0m0.001s
vector operations used in the second executable, but gain in time is not such a big.
Posted by
Roman G.
at
12:31 AM
0
comments
Labels: C, C++, C99, CentOS, GNU/Linux, Language comparison, performance
Thursday, July 10, 2008
stupid vs smart, case vs ftable, algorithms vs processors, testing price vs gain
Actually, the task below can be easily solved by the paper and pen.
It was written in home at evening, just to measure performance as additional argument for rewriting stupid code for improving maintainability and performance of old legacy code.
Hypothetical example of the optimization of the following code was considered, and the return of the optimization effort (as resulting 'optimized' code) was timed.
for(unsigned int i=0;i<65536;i++)
for(unsigned int j=0;j<65536;j++)
switch(j%15+1)
{
case 1:
f1();
break;
case 2:
f2();
...
}
The following are examples of performance, that can be achieved for integer massive operations due to different types of optimization.
Tried on AMD dual core DeskTop centOS, with gcc 4.1.3,
with optimization, and without.
4 types of code were considered:
Using switch(code) and case to define function to call;
Using case(code) and inlining function in call;
Using call to function by code ( fun(code) );
Just doing job without obfuscation by ftabs and cases.
Compilation done in following way:
[rtg@rtgCent cpp_exa]$ g++ -O3 case.cpp -o caseo
[rtg@rtgCent cpp_exa]$ g++ -O3 ftab.cpp -o ftabo
[rtg@rtgCent cpp_exa]$ g++ ftab.cpp -o ftab
[rtg@rtgCent cpp_exa]$ g++ case.cpp -o case
[rtg@rtgCent cpp_exa]$ su -
Some examples of output:
Red and green just presents timing for worst and best timing.
[root@rtgCent cpp_exa]# time nice -n -10 ./case
-458752
real 2m23.422s
user 2m23.205s
sys 0m0.112s
[root@rtgCent cpp_exa]# nice -n -10 ./caseo
-458752
[root@rtgCent cpp_exa]# time nice -n -10 ./caseo
-458752
real 1m3.688s
user 1m3.614s
sys 0m0.054s
[root@rtgCent cpp_exa]# time nice -n -10 ./ftab
-458752
real 1m41.664s
user 1m41.521s
sys 0m0.101s
[root@rtgCent cpp_exa]# time nice -n -10 ./ftabo
-458752
real 0m56.068s
user 0m56.004s
sys 0m0.047s
[root@rtgCent cpp_exa]# time nice -n -10 ./plaino
-458752
real 1m3.801s
user 1m3.732s
sys 0m0.047s
[root@rtgCent cpp_exa]# time nice -n -10 ./plain
-458752
real 1m53.174s
user 1m53.060s
sys 0m0.079s
[root@rtgCent cpp_exa]# time nice -n -10 ./opto
-458752
real 0m14.527s
user 0m14.509s
sys 0m0.014s
[root@rtgCent cpp_exa]# time nice -n -10 ./opt
-458752
real 0m31.956s
user 0m31.923s
sys 0m0.022s
It were just examples of the simpliest 'non algorithmic' optimization.
Simplified 'Algorithmic' optimization of the simplest case gives more than 100 time's faster output:
[root@rtgCent cpp_exa]# time nice -n -10 ./alg
-458752
real 0m0.011s
user 0m0.004s
sys 0m0.005s
[root@rtgCent cpp_exa]# time nice -n -10 ./algo
-458752
real 0m0.011s
user 0m0.003s
sys 0m0.005s
If you have some different opinion/results, please report in comments :)
By the way, who needs optimization today of critical systems, with today's pricing on retesting of system, and today modern cheap fast processors?
Code for examples:
alg.cpp
ftab.cpp
case.cpp
plain.cpp
opt.cpp
cpuinfo:
cpuinfo
Posted by
Roman G.
at
11:00 PM
1 comments
Labels: C, C++, CentOS, GNU/Linux, Language comparison, performance
Wednesday, July 9, 2008
atexit() example. exit (1) in main, but $?=0
This atexit function just look like another way to obfuscate programs.
Example:
[rtg@rtgCent cpp_exa]$ cat atex.c
#include <stdio.h>
#include <stdlib.h>
void mine(void)
{
printf("mine!\n");
exit(0);
}
void yours(void)
{
printf("yours!\n");
exit(1);
}
int main(int argc,char **argv )
{
atexit(mine);
atexit(yours);
printf("exiting...\n");
exit(1);
}
[rtg@rtgCent cpp_exa]$ gcc atex.c
[rtg@rtgCent cpp_exa]$ ./a.out
exiting...
yours!
mine!
[rtg@rtgCent cpp_exa]$ echo $?
0
Posted by
Roman G.
at
12:31 AM
0
comments
Labels: C++, GNU/Linux, Language comparison
Tuesday, July 8, 2008
c99 vectors, c++ example.
C99 vectors example .c file.
vector's some, and way to view as int's (through union).
C++ references as class members (with initialization, that should be done ), static counter of objects, array, and copy constructor (have to be?) .cpp file.
Posted by
Roman G.
at
2:07 AM
0
comments
Labels: C, C++, C99, concepts, Language comparison
Thursday, July 3, 2008
setjmp, longjmp second parameter
setjmp, longjmp looks like continuations in functional languages.
This functions save/restore call contest as it does call/cc.
modified example from there:
http://publications.gbdirect.co.uk/c_book/chapter9/nonlocal_jumps.html
return values (second argument of longjmp example)
[rtg@rtgCent example]$ vi _jmp.c
[rtg@rtgCent example]$ gcc -g _jmp.c -o jmp
[rtg@rtgCent example]$ ./jmp
inside jmp: func
place retval=1
inside jmp: func2
place 2 retval=32
despite of longjmp second parameter set to zero, retval displayed as 1.
Here is example wich shows global and local variables in caller contest change.
_jmp2.c
Posted by
Roman G.
at
12:40 AM
0
comments
Labels: C, C++, GNU/Linux, Language comparison
Monday, May 19, 2008
GUI vs. Command Line, similiarity in tools
My first long-term job was Unix programming (in despite of fact that first money for code I got for Z-80 chip programming (some little hack of Z80 in beginning of 90'th )).
Thats way I prefer the command line tools.
I think GUI does not have real performance in tasks of programmer, also GUI skills can not be reused when you need automate your task you done before by clicking.
If you typed instead, you can put your typing in the script and make the script working for any count of times.
However I found in my practice some cases when GUI was better than command line for development.
1) Development of GUI
I have experience of the development of tools for the internet trading, that were mouse based and highly interactive. And visual tools for visual development were really much progress in comparison to writing code for forms definition. In such RAD tools for the GUI, GUI interface and the visual development environment is the must.
However, I know that for experienced traders command line tools were much more comfortable.
They used command line tools for trading instead of GUI, if they had the choice.
2) GUI for understanding new tool.
Visual GUI wrapper around CLI can be helpful when you are learning new tool.
Of cause output/actions log window in the CLI form is the must, to be able than do the actions from the command line.
3) GUI as wrapper around family of CLI tools, that are based on some concept, however using different syntax. For example, uniform interface around sccs/rcs/cvs/svn/git/ and so on,
helps you to avoid switching contest errors, when you have to maintain systems, based on different version control tools.
However, CLI wrapper would be even more helpful :)
Posted by
Roman G.
at
8:41 AM
0
comments
Labels: concepts, GNU/Linux, Language comparison, usability
Friday, May 2, 2008
Found clone finding app
Found following utility for refactoring:
CCFinderX : http://www.ccfinder.net/
It works well with SUN java6 used by default on my Xubuntu Hardy laptop.
manual also available on site.
Posted by
Roman G.
at
12:59 PM
0
comments
Labels: GNU/Linux, Language comparison, Ubuntu
Tuesday, April 22, 2008
refactoring and spaghetti code
from engineering experience:
- Using map instead of repeating calls for function with more than two parameters looks bad and unmaintainable for number of calls 5 or less.
- Using dynamic SQL for creation of similar tables , describing same parts in while loop looks bad if number of tables less than 4.
Of cause you have not use this tech's if the code is evolving and probably would be changed (or it is permanent solution).
Posted by
Roman G.
at
6:02 AM
0
comments
Labels: GNU/Linux, Language comparison, perl, SQL
Saturday, March 22, 2008
javascript conditional function definition (c #ifdef ?)
C:
#ifdef MOZILLA_OS
#define _XHTML_
#endif
#ifdef _XHTML_
int print()
{
printf("<node />");
}
#endif
#ifndef _XHTML_
int print()
{
printf("<node></node>");
}
#endif
Javascript:
var OS="MOZILLA";
var XHTML;
if(OS=="MOZILLA")
{
XHTML=1;
} else {XHTML=0;};
if (XHTML)
{
function print() {document.write("<node />");};
} else {
function print() {document.write("<node></node>");};
};
Posted by
Roman G.
at
10:36 PM
0
comments
Labels: C, javascript, Language comparison
Saturday, March 15, 2008
Logic operators: Lazyness C++, Perl, Java, Javascript, ksh
Examples demonstrating lazy evaluation for || and && in different languages presented.
Consider following c++ code:
Fun1() || Fun2() || Fun3();
Fun1() && Fun2() && Fun3();
In c++, they are executed, only if the result of overall execution depends from execution of function that was not yet executed. '&&' , '||' and ',' are only c++ operators, thats order is direct from left-to right.
In examples:
Java1.6:
/**
* @author rtg
*
*/
public final class main {
public static boolean fun1(){System.out.println("fun1"); return true;}
public static boolean fun2(){System.out.println("fun2"); return false;}
public static boolean fun3(){System.out.println("fun3"); return true;}
/**
* @param args
*/
public static void main (String[] args) {
boolean res;
System.out.println("|| test:");
res = fun1() || fun2() || fun3();
System.out.printf("res:%b\n",res);
System.out.println("&& test:");
res = fun1() && fun2() && fun3();
System.out.printf("res:%b",res);
}
}
Results are the same as for
Javascript in firefox 2 is lazy too:
example
Perl's operators are lazy too:
$ cat lazy_or.pl
#!/usr/bin/perl
sub fun1 {print "fun1\n"; return 1;};
sub fun2 {print "fun2\n"; return 0;};
sub fun3 {print "fun3\n"; return 1;};
sub main
{
print "or:\n";
print "res=" . (fun1() or fun2() or fun3()) . "\n";
print "and:\n";
print "res=" . (fun1() and fun2() and fun3()) . "\n";
print "&&:\n";
print "res=" . (fun1() && fun2() && fun3()) . "\n";
print "||:\n";
print "res=" . (fun1() || fun2() || fun3()) . "\n";
}
main();
rtg@kubic-roman:~$ perl lazy_or.pl
or:
fun1
res=1
and:
fun1
fun2
res=0
&&:
fun1
fun2
res=0
||:
fun1
res=1
rtg@kubic-roman:~$ perl -v
This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
ksh example at the end is different in results from perl c++ and javascript.
After ksh testing I understand, why Java not accepts int's as booleans for the operator.
ksh interprets 0 as true and 1 as false, so it is the place were you have to be patient,
when swith to:
$ cat lazy.ksh
#!/usr/bin/ksh
fun1() {
echo "fun1"
return 1;
}
fun2() {
echo "fun2"
return 0;
}
fun3() {
echo "fun3"
return 1;
}
status(){
echo $?
}
fun1 || fun2 || fun3
status
fun1 && fun2 && fun3
status
rtg@kubic-roman:~$ ./lazy.ksh
fun1
fun2
0
fun1
1
Thats strange, that a lot of programmers in presented languages where very confident about non-laziness of the operators.
However, it is common for all presented languages, and it is no conceptual difference in these languages at the point described.
C++ example:
#includeresult of code execution is:
int fun1(){std::cout << "fun1\n"; return 1;}
int fun2(){std::cout << "fun2\n"; return 0;}
int fun3(){std::cout << "fun3\n"; return 1;}
int main()
{
int test_val;
std::cout << "\n || test:\n";
test_val = fun1() || fun2() || fun3();
std::cout << "result is:" << test_val << std::endl;
std::cout << "\n && test:\n";
test_val = fun1() && fun2() && fun3();
std::cout << "result is:" << test_val << std::endl;
}
|| test:
fun1
result is:1
&& test:
fun1
fun2
result is:0
Posted by
Roman G.
at
2:34 PM
0
comments
Labels: C++, java, javascript, ksh, Language comparison, perl, shell
Monday, February 25, 2008
Links: differences in syntax: Perl, tcl, Shell, C++, Python, Java, Javascript, Lisp
I have already mentioned in my post on comparing numbers and strings in shell, about problems of simultaneous use of different programming languages.
To conclude, having reference cards with description one language to another differences can be useful.
Listed is set of resources, intended for migration from one language to another.
Recommended (short,self-descriptive, useful):
languages comparison:
http://merd.sourceforge.net/pixel/language-study/syntax-across-languages/
Recommended, but not short:
Wikipedia page:
http://en.wikipedia.org/wiki/Comparison_of_programming_languages
Open directory listing for comparisons:
http://www.dmoz.org/Computers/Programming/Languages/Comparison_and_Review/
PLEAC - Programming Language Examples Alike Cookbook
Comparison of productivity of writing in different programming languages:
page.mi.fu-berlin.de/~prechelt/Biblio/jccpprtTR.pdf
Useful:
Java for c++ Programmers:
http://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial/
http://triton.towson.edu/~mzimand/os/Lect2-java-tutorial.html
Lisp to javascript converter, descriptive.
http://javascript.crockford.com/little.html
Another one Lisp to Javascript converter written in javascript.( You can look into source to look into the code)
http://www.joeganley.com/code/jslisp.html
Comparison Python with Java, Lisp i.t.c.
http://wiki.python.org/moin/LanguageComparisons
Three scripting concurrents:
http://mjtsai.com/blog/2002/11/25/perl_vs_python_vs_ruby/
Accumulator generator in different languages:
http://www.paulgraham.com/accgen.html
Tcl vs. Python, with nice short examples
http://homepages.cwi.nl/~sjoerd/PythonVsTcl-old.html
This thread describes differences between bash and perl.
http://www.perlmonks.org/?node_id=661859
And at the end resource with language comparison in action (memory, speed, size).
http://shootout.alioth.debian.org/
Posted by
Roman G.
at
8:40 PM
0
comments
Labels: C, C++, java, javascript, Language comparison, Lisp, perl, Python, Tcl
Thursday, February 21, 2008
Compare strings and numbers in shell
Comparison operators and syntaxes of comparisons are different for different languages. For example 'eq' keyword in perl used in comparisons of strings, and in shell '-eq' used for comparisons of numbers.
The more interesting thing, that even for bash ( most often used as interactive shell) and ksh (used for scripts), results of comparisons would be different for absolutly the same expression.
Example is:
$ ksh
$ set -o vi #for more convinient command-line editing
$ if [ "b " -eq " b" ] ; then echo "hurray" ; else echo "dfdfd" ; fi
hurray
$ bash
rtg@kubic-roman:~$ if [ "b " -eq " b" ] ; then echo "hurray" ;
else echo "dfdfd" ; fi
bash: [: b numerical expression expected
dfdfd
Incorrect comparison used; compared strings as numbers. Got two different tests results for .ksh and bash, and bash reported problem, but .ksh not.
Now fix expression, and use "=" operator as specified.
~$ ksh
$ set -o vi
$ if [ "b " = " b" ] ; then echo "equal" ; else echo "non equal" ; fi
non equal
$ bash
rtg@kubic-roman:~$ if [ "b " = " b" ] ; then echo "equal" ; else echo "non equal" ; fi
non equal
It is correct, and same for both expressions.
What will be, if numerical comparison used for string containing numbers?
It is more simple to display it in script:
$ ksh
$ if [ "7" -eq ' 7 ' ] ; then echo "equal" ; else echo "non equal" ; fi
equal
$ if [ "7" == ' 7 ' ] ; then echo "equal" ; else echo "non equal" ; fi
non equal
the same results are in the bash.
Consclusion:
If move from another language, especially statically typed like C:
- be patient with scripts (especially comparisons)
- if a script does not report any error, it still can be buggy; C program probably won't even compile for cases processed by shell script with hidden error.
- If some command in script failed, and even produced error message, script continues execution if else not specified in the script. Some of the errors of this type could be avoided by always checking result of command execution after each command in script.
- Test cases are needed, especially when migrate/upgrade to new toolset/system, even if application code is unchanged.
Why this post have been written:
Switching from one programming language to the another is popular.
It is because modern systems are heterogeneous.
The problem: often the same entity in contest of one programming language, present something complete different in contest of another language.
It is simple to use only one language. You can be sure in such a case, that when you are coding automatically, you are using exact syntax from the language used, but not from some another language.
It is complex, when use a lot of languages.
When switching from one language to another, looking through short reference card of language you switched from, and that of language you switched in, can be useful. It can help to place yourself in right contest, and get rid of previous contest.
Perl and Shell scripts, that are often similar in construction, have different variable declaration, but the same definition (shell does not use $ when defining variable), tests are different, function definitions i.t.c.
Posted by
Roman G.
at
11:52 PM
0
comments
Labels: bash, ksh, Language comparison, shell
Tuesday, February 19, 2008
Javascript equivalent of perl attributes.
This javascript enabled page contains code for this article
It was checked under Mozilla Firefox 2.0.0.12
In one of my previous posts I have considered perl attributes.
Attributes are functions itself,that could be defined by user, and that have access to another function body, and can operate another functions, at the moment of definition of the functions with the attributes.
For example, when such definition met by Perl in the Perl script,
sub func_sub : attribute_func Perl executes attribute_func, passing func_sub to the function. Then attribute_func can change func_sub executable body, adding "header" and "footer" to the function, or alerting about definition met, for example.Due to the fact, that JavaScript is also used widely for web applications, but on client side,
I have asked myself about equivalent JavaScript form.
It appeared, that for JavaScript it is even more simple equivalent exists, that can be used for understanding Perl attributes definition. Attribute can be implemented as high order function, that manipulates function body.
We can log function in and out using following attribute function defined as following closure:
function attr (fparam){
document.writeln("attr.beginning< br >");
fparam();
document.writeln("attr.end< br >");
}
Example of how it can be applied:
a = function(var_my)
{
attr(function()
{
document.writeln("this is first line of original function (a) < br >");
document.writeln("var passed:" + var_my + "< br >");
} )
};
document.writeln("perform call to a() < br >");
a("1");
More complex example of attribute , that modifies code of function
before function will be executed:
We define attribute that prints function's code given, and that adds additional strings of code to function, and define this new function under the different name, and than executes modified function.
It is example of self-modifying code.
// prints a lot of debug info,
// define new function like given, but with changes in body
// and execute it.
function attr2 (fparam){
document.writeln("attr.beginning < br >");
document.writeln("func get:" + fparam +"< br >");
var tempstr = new String;
tempstr = fparam.toString();
tempstr = tempstr.replace(");","); global_var*=2;
document.writeln(\"inserted by attr2\");" );
tempstr = tempstr.replace("function ()",
"function b_modified()");
var func = tempstr;
document.writeln("Now call func made by attr2:"
+ tempstr + "< br >");
eval(func);
b_modified();
document.writeln("attr.end< br >");
}
// define b as attr2(anonimous function given)
b = function()
{
attr2(function()
{
document.writeln("this is first line of original function (b)< br >");
document.writeln("global_var="+ global_var);
} )
};
// global_var is printed and modified inside b
var global_var=2;
b();
In the begining of article link to the place were all the code cited
can be viewed in action published.
Posted by
Roman G.
at
9:26 PM
0
comments
Labels: attributes, javascript, Language comparison, perl
Sunday, February 17, 2008
Perl nested lists vs. Python and Lisp.
It is no nested lists in Perl. However, Python and Lisp have this ability.
Description of implementing in Perl equivalent structure to nested Python and Lisp lists given.
Used:
$ python --version
Python 2.5.1
$ perl -v | head -2
This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
$ clisp --version | head -1
GNU CLISP 2.41 (2006-10-13) (built 3371977993) (memory 3401903509)
Consider
'(1 (2 (3) 4) 5)lisp list as the example of data structure we work with:
$ clisp
...
[1]> (setq alist `(1 (2 (3) 4) 5))
(1 (2 (3) 4) 5)
[2]> (elt alist 0)
1
[3]> (elt alist 2)
5
[4]> (elt alist 1)
(2 (3) 4)
[5]>
Corresponding interactive session for Python:
>>> a = [1, [2, [3], 4], 5]
>>> print a
[1, [2, [3], 4], 5]
>>> print a[0]
1
>>> print a[1]
[2, [3], 4]
>>> print a[2]
5
Python in list processing is very like Lisp. The difference for defining list body is only using commas and square brackets in python, and you don't need put quote sign before it. (As done in Lisp to interpret it as list, not executing as function).
Perl is different in this point. Perl instead of interpreting list as given, removes all parens inside.
$ perl -e '@al = (1, (2, (3), 4), 5); \
print "\@al[0]=@al[0] , \@al[1]=@al[1] , \@al[2]=@al[2] ,\
\@al[3]=@al[3] , \@al[4]=@al[4] \n"; \
print @al; print "\n"; '
@al[0]=1 , @al[1]=2 , @al[2]=3 , @al[3]=4 , @al[4]=5
12345
Lisp has very convenient data model, all data in lisp are pointers.
And the solution of implementing nested lists in Perl is pointer based.
Pointer to list in perl uses square brackets ([]), like lists syntax in Python.
So our structure look is:
$al = [1, [2, [3], 4], 5];
Following procedure prints elements, emulating output in Python session and Common Lisp REPL:
sub slistp($)
{
my ($listp) = @_;
my $i;
my $ret = "";
if (ref($listp) eq 'ARRAY')
{
$ret = "[";
$ret .= slistp(@$listp[0]);
for ($i=1;$i<(scalar @$listp);$i++)
{
$ret .= ",";
$ret .= slistp(@$listp[$i]);
};
$ret .= "]";
}
else
{
$ret .= $listp;
}
return($ret);
}
Usage for our list:
$al = [1, [2, [3], 4], 5];
print "\@al[0]=".@$al[0].", \@al[1]=@$al[1] , \@al[2]=@$al[2] \n";
print slistp($al);
print "\n";
print "\@al[0]=".slistp(@$al[0])."\n\@al[1]=".slistp(@$al[1]);
print "\n\@al[2]=".slistp(@$al[2])."\n";
result of complete code execution:
$ perl nestlist.pl
@al[0]=1, @al[1]=ARRAY(0x8152b44) , @al[2]=5
[1,[2,[3],4],5]
@al[0]=1
@al[1]=[2,[3],4]
@al[2]=5
So for Perl lisp-based tree structures, list references have to be used instead of lists.
Additional fees for dereferencing array pointers applied in Perl syntax.
It is no REPL integrated session in Perl, and examining complex structures based on lists require usage of additional modules, or writing own functions. Therefore Python, and especially Lisp are more preferable for list processing tasks.
Posted by
Roman G.
at
3:31 PM
0
comments
Labels: Common Lisp, Language comparison, perl, Python
Friday, February 8, 2008
Common Lisp vs Python: code size
Just joke :)
I know, it is not enough statistics for conclusions :)
We have compared reduce statement in Common lisp and python with one fellow.
I wrote in Common lisp, he in Python.
Resulting "code" sizes for same construct, just translated from one language to another:
$ echo "reduce(lambda x,y: (x,y,x), (1,2,3,4))" | wc
1 4 39
$ echo "(reduce (lambda(x y)(list x y x)) (list 1 2 3 4))" | wc
1 11 50
Python : 39 symbols long
Common Lisp : 50 symbols.
And the winner is Python :)
Python win about 20 or 25 percents of code size.
Posted by
Roman G.
at
7:04 PM
0
comments
Labels: Common Lisp, Language comparison, Python