Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Sunday, April 17, 2022

dlang vs rust comparison (based on alimg)

Comparing dmd, ldc, rust based on the same algorithm implementation:

https://github.com/exhu/alimg/tree/master/dmd

https://github.com/exhu/alimg/tree/master/bufdither-rust


rust version is the fastest (2.5 s) vs dmd's 5 secs, ldc's 2.9 s.

rust version also consumes less memory:

Maximum resident set size (kbytes): 3716

Elapsed (wall clock) time (h:mm:ss or m:ss): 0:02.50
 

DMD: 

Maximum resident set size (kbytes): 5380

Elapsed (wall clock) time (h:mm:ss or m:ss): 0:05.28

LDC: 

Elapsed (wall clock) time (h:mm:ss or m:ss): 0:02.91

Maximum resident set size (kbytes): 8044

 

The D programming language version turned out to be easy and quick to implement as it was very close to Java/C++.

The Rust version took very long time to implement, as it required considerably more time to learn Rust's basics.

Wednesday, June 23, 2021

Text templates, code generation

 Python: wheezy.template

@def indent(lines, sep):
@sep.join(lines.split('\n'))
@end

@def body():
function body;
statement2;
@end

@def func(name):
    func @name {
        @body()
        @indent('1\n2\n3\n', '\n        ')
        @indent(body(), '\n        ')
    }
@end


@func('hello')


Java: StringTemplate4

Sunday, February 7, 2021

Julia programming language notes

 julialang.org

https://docs.julialang.org/en/v1/manual/unicode-input/

https://benlauwens.github.io/ThinkJulia.jl/latest/book.html

 

Documentation is not clear,  big memory footprint (125MB RAM), slow to start first time on Windows.

The language is dynamic, no way to specify exactly function arguments and return value for a type (e.g. as a type of an argument to another function, or a type of a variable holding a function object).

An error can be spotted only during execution, e.g. if the return type does not match the type of the expression returned from the function.

Monday, July 3, 2017

Evaluating Lua

Three active verisons 5.1, 5.2, 5.3. LuaJIT as 5.1 with some nice C integration features.
The most viable is 5.1 because of luajit/lua implementation choice.

Simple scripts startup time is almost the same as a C program (python is way slower to start).
Memory consumption is low. Several interpreters can be run in the same process.
Syntax is simple and yet allows to be used for DSLs, sandboxing.

RTTI is almost not available apart from discovering if the object is a function, a string, a number or a table.
Standard library is almost non-existing, except for Penlight package from LuaRocks package manager.

Documenting tools are rather lacking and buggy (tried LDoc).

Great for prototyping but as soon as a program grows beyond a couple of modules, it needs proper docs tools, higher level objects (OOP) etc. Existing solutions are quite poor.

Could not find a good REPL implementation.

Monday, October 14, 2013

Scheme as embeddable language for C

Tried guile 2.0.9, tinyScheme, chibi-scheme and lua 5.2.

On MacOSX 10.8.5 memory footprint of default interpreter is:

guile 12.4 MB,
tinyScheme 984 KB,
chibi-scheme 2.7 MB,
lua 760 KB.

Executing 'hello world' with 'time' takes:

time guile -c '(display "hello, world!\n")' --- real 0m0.027s
time ./scheme -c '(display "hello, world!\n")' --- real 0m0.007s
time ./chibi-scheme -e '(display "hello, world!\n")' --- real 0m0.046s
 time lua -e 'print("hello, world!\n")' --- real 0m0.004s

 To sum up, for config files tiny-scheme or lua are perfect. Taking into account that lua is a full-featured language while tiny-scheme is a shrinked down one, lua is a clear winner.

Saturday, June 22, 2013

What every programmer should know about memory

http://lwn.net/Articles/250967/ Software optimization resources http://www.agner.org/optimize/

Friday, May 31, 2013

2D math, fixed point, angles etc.

http://www.helixsoft.nl/articles/circle/sincos.htm

Thursday, May 30, 2013

Multithreading in FLTK

The very smart way: http://www.fltk.org/doc-1.3/advanced.html
int main() {
      Fl::lock();
      /* run thread */
      while (Fl::wait() > 0) {
        if (Fl::thread_message()) {
          /* process your data */
        }
      }
    }

//////////////
void *msg;       // "msg" is a pointer to your message
Fl::awake(msg);  // send "msg" to main thread

//////////////

void do_something(void *userdata) {
      // running with the main thread
    }

    // running in another thread
    void *data;       // "data" is a pointer to your user data
    Fl::awake(do_something, data);  // call something in main thread

Tuesday, April 30, 2013

Linux low-level GUI graphics

X11: xlib for window and events, XCB for low-level asynchronous, XRender extension for modern 2D graphics, Xft library for fonts rendering. More to read: http://blog.mecheye.net/2012/06/the-linux-graphics-stack/

Wednesday, October 3, 2012

Lock-free queue benchmark

My Lock and lock-free queue benchmark in Java: https://github.com/exhu/miscalg/tree/master/lockfreej Possibly contains ABA problem, but did not show itself while tests -)

Tuesday, October 2, 2012

online compiler/disasm

http://gcc.godbolt.org/

Friday, August 31, 2012

Defective C++ FQA

http://yosefk.com/c++fqa/defective.html

Monday, August 27, 2012

Lock-free, wait-free, hazard pointers

Articles on the topic: http://www.drdobbs.com/lock-free-data-structures-with-hazard-po/184401890 http://www.research.ibm.com/people/m/michael/ieeetpds-2004.pdf http://www.ibm.com/developerworks/java/library/j-jtp10264/

Tuesday, August 14, 2012

Useful links

http://graphics.stanford.edu/~seander/bithacks.html http://fgiesen.wordpress.com/2011/01/17/texture-tiling-and-swizzling/ http://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/

Useful geometry functions -)

void ProjectPointOnPlane(const Vector3 & p, const Plane & plane, Vector3 & projected)
{
    float d = plane.DistanceToPoint(p);
    projected = p - d*plane.n;
}


float CalcVectorAngle(const Vector2 & a, const Vector2 & b)
{
    float c = atan2f(b.y,b.x) - atan2f(a.y,a.x);

    if (c < -PI)
        c += 2.f*PI;
    else
        if (c > PI)
            c -= 2.f*PI;

    return c;
}


Friday, June 15, 2012

Nimrod macro

So finally some macro stuff in Nimrod programming language I managed to write.
import macros

when false:
    dumpTree:
        type
            PMyObj* = ref TMyObj 
            TMyObj* = object of TObject
                a, b : int

        proc newTMyObj*() : PMyObj =
            var o : PMyObj
            new(o)
            #initTMyObj(o[])
            return o    
    
#proc initTMyObj*(o : var TMyObj) = nil
    
when false:
    proc newTMyObj*() : PMyObj =
        var o : PMyObj
        new(o)
        #initTMyObj(o[])
        return o
    
    
macro objdecl(clsbase : expr) : stmt =
    result = newNimNode(nnkStmtList)#, clsbase)
    #for i in 1..clsbase.len-1:
    #echo(repr(clsbase[i]))
    let clsName = repr(clsbase[1])
    let baseName = repr(clsbase[2])
    let tname = "T" & clsName
    let pname = "P" & clsName
    echo("base = " & baseName & ", type = " & tname)
    var fields = clsbase[3][0][0] # [0] skip stmt list, [0] skip var section
    
    
    echo("fields = " & repr(fields))
    
    
    # start 'type' section
    var tsection = newNimNode(nnkTypeSection)
    result.add(tsection)
    
    block:
        # define 'type' node for Pclass * = ref Tclass
        var tnode = newNimNode(nnkTypeDef)
        tsection.add(tnode)
        
        # define type name with * operator
        var tpostf = newNimNode(nnkPostfix)
        tpostf.add(newIdentNode("*"))
        tpostf.add(newIdentNode(pname))
        tnode.add(tpostf)
        
        # don't know why but need an empty node
        tnode.add(newNimNode(nnkEmpty))
        
        # the actual type = ref
        var reft = newNimNode(nnkRefTy)
        reft.add(newIdentNode(tname))
        tnode.add(reft)
    
    
    block:
        # define 'type' node for Tclass * = object of base
        var tnode = newNimNode(nnkTypeDef)
        tsection.add(tnode)
        
        # define type name with * operator
        var tpostf = newNimNode(nnkPostfix)
        tpostf.add(newIdentNode("*"))
        tpostf.add(newIdentNode(tname))
        tnode.add(tpostf)
        
        # don't know why but need an empty node
        tnode.add(newNimNode(nnkEmpty))
        
        # the actual type = object of
        var objt = newNimNode(nnkObjectTy)
        tnode.add(objt)
        
        # needed, don't know why
        objt.add(newNimNode(nnkEmpty))
        
        var inh = newNimNode(nnkOfInherit)
        objt.add(inh)
        inh.add(newIdentNode(baseName))
        
        var recs = newNimNode(nnkRecList)
        objt.add(recs)
        
        recs.add(fields)
        
        #var identDefs = newNimNode(nnkIdentDefs)
        #recs.add(identDefs)
        
        #identDefs.add(getAST(fields))
        #for i in 0..len(fields)-1:
        #    identDefs.add(fields[i])
            
            
        # needed, don't know why
        #identDefs.add(newNimNode(nnkEmpty))
        
    
    #echo(fields.len)
    echo(treeRepr(tsection))
    #echo(repr(tsection))
    
    
    
    
    
    #tnode.add(new
    
    
template declClass(clsName : expr, baseCls : expr, fields : stmt) : stmt = 
    
    
    objdecl(clsName, baseCls, fields)
    when false:
        #`P clsName`* = ref `T clsName`
        #`T clsName`* = object of baseCls
        #    putfields

    proc `new clsName`() : `P clsName` =
        var o : `P clsName`
        new(o)
        return o
        
    
        
# ----- the usage:
    
 
declClass MyTempClass, TObject:
    var x, y : int
    

      
var o = newMyTempClass()
o.x = 3
echo("x,y = " & $o.x & " " & $o.y)

Wednesday, May 23, 2012

Nimrod programming language

Very interesting language, spotted when looking for Vala (generates C code) -)

http://nimrod-code.org/index.html

Tuesday, May 22, 2012

Java image editor

With sources for filters etc.

http://www.jhlabs.com/ie/