Re: GTK 3.20 Nim wrapper

2016-12-15 Thread vlad1777d
Thanks, all went normally, but I stacked with creating pixbuf from image.

I decided to use this: 
[https://pp.vk.me/c638428/v638428447/13ff8/5by3ijiErV4.jpg](https://pp.vk.me/c638428/v638428447/13ff8/5by3ijiErV4.jpg)
 function, but I got an error: 
[https://pp.vk.me/c638428/v638428447/13fef/309fNgF4xlc.jpg](https://pp.vk.me/c638428/v638428447/13fef/309fNgF4xlc.jpg)
 .

I searched in manuals how to fix it, tried many ways: 
[https://pp.vk.me/c638428/v638428447/13fe6/bom-MQo64JQ.jpg](https://pp.vk.me/c638428/v638428447/13fe6/bom-MQo64JQ.jpg)
 
[https://pp.vk.me/c638428/v638428447/1400a/iYzeqZ_XEzI.jpg](https://pp.vk.me/c638428/v638428447/1400a/iYzeqZ_XEzI.jpg)
 , but I have not found that enumeration (from last screenshot) in .nim wrapper 
files.

I tried to use it directly, but I got error anyway: 
[https://pp.vk.me/c638428/v638428447/14001/vbiPiJPxcxA.jpg](https://pp.vk.me/c638428/v638428447/14001/vbiPiJPxcxA.jpg)

So I give up. Maybe you could make me some help?)


Re: unescape \n \r etc in string

2016-12-15 Thread vlad1777d
@Krux02, maybe this is a bug, you could post an issue on GitHub.


Re: unescape \n \r etc in string

2016-12-15 Thread Krux02
no, I am asking for the inverse of that function. There is unescape, but it 
doesn't do what I want to do:


import strutils

let str = r"\n\r"

echo str
assert unescape(str, "", "") == "\n\r"



Re: Fun with rdtsc() microbenchmarks

2016-12-15 Thread Stefan_Salewski
Well, more meaningful and even more funny is this code with random numbers, 
where the compilers really can not remove the proc calls:


import random
proc rdtsc(): int64 =
  var hi, lo: uint32
  asm """
rdtsc
:"=a"(`lo`), "=d"(`hi`)
  """
  result = int64(lo) or (int64(hi) shl 32)

proc sort1(d: var openarray[int]) {.inline.} =
  template SWAP(x, y: int) =
let a = min(d[x], d[y])
let b = max(d[x], d[y])
d[x] = a
d[y] = b
  SWAP(1, 2)
  SWAP(4, 5)
  SWAP(0, 2)
  SWAP(3, 5)
  SWAP(0, 1)
  SWAP(3, 4)
  SWAP(1, 4)
  SWAP(0, 3)
  SWAP(2, 5)
  SWAP(1, 3)
  SWAP(2, 4)
  SWAP(2, 3)

proc sort2(d: var openarray[int]) =
  template SWAP(x, y: int) =
if d[x] > d[y]: swap(d[x], d[y])
  
  SWAP(1, 2)
  SWAP(4, 5)
  SWAP(0, 2)
  SWAP(3, 5)
  SWAP(0, 1)
  SWAP(3, 4)
  SWAP(1, 4)
  SWAP(0, 3)
  SWAP(2, 5)
  SWAP(1, 3)
  SWAP(2, 4)
  SWAP(2, 3)

proc testIt =
  var a = [0,1,2,3,4,5]
  var b = [5,4,3,2,1,0]
  var x: array [6, int]
  var now, start: int64
  
  for i in 0 .. 5: x[i] = random(6)
  start = rdtsc()
  sort1(x)
  now = rdtsc()
  echo(repr(x))
  echo "Cycles: " & $(now - start)
  
  for i in 0 .. 5: x[i] = random(6)
  start = rdtsc()
  sort1(x)
  now = rdtsc()
  echo(repr(x))
  echo "Cycles: " & $(now - start)
  
  for i in 0 .. 5: x[i] = random(6)
  start = rdtsc()
  sort2(x)
  now = rdtsc()
  echo(repr(x))
  echo "Cycles: " & $(now - start)
  
  for i in 0 .. 5: x[i] = random(6)
  start = rdtsc()
  sort2(x)
  now = rdtsc()
  echo(repr(x))
  echo "Cycles: " & $(now - start)

when isMainModule:
  testIt()



[0, 2, 2, 5, 5, 5]
Cycles: 36
[0, 0, 2, 2, 4, 5]
Cycles: 36
[0, 2, 2, 3, 4, 4]
Cycles: 140
[0, 0, 0, 3, 3, 5]
Cycles: 164



Fun with rdtsc() microbenchmarks

2016-12-15 Thread Stefan_Salewski

proc rdtsc(): int64 =
  var hi, lo: uint32
  asm """
rdtsc
:"=a"(`lo`), "=d"(`hi`)
  """
  result = int64(lo) or (int64(hi) shl 32)

proc sort1(d: var openarray[int]) =
  template SWAP(x, y: int) =
let a = min(d[x], d[y])
let b = max(d[x], d[y])
d[x] = a
d[y] = b
  SWAP(1, 2)
  SWAP(4, 5)
  SWAP(0, 2)
  SWAP(3, 5)
  SWAP(0, 1)
  SWAP(3, 4)
  SWAP(1, 4)
  SWAP(0, 3)
  SWAP(2, 5)
  SWAP(1, 3)
  SWAP(2, 4)
  SWAP(2, 3)

proc sort2(d: var openarray[int]) =
  template SWAP(x, y: int) =
if d[x] > d[y]: swap(d[x], d[y])
  
  SWAP(1, 2)
  SWAP(4, 5)
  SWAP(0, 2)
  SWAP(3, 5)
  SWAP(0, 1)
  SWAP(3, 4)
  SWAP(1, 4)
  SWAP(0, 3)
  SWAP(2, 5)
  SWAP(1, 3)
  SWAP(2, 4)
  SWAP(2, 3)

proc testIt =
  var a = [0,1,2,3,4,5]
  var b = [5,4,3,2,1,0]
  var x: array [6, int]
  var now, start: int64
  
  x = a
  start = rdtsc()
  sort1(x)
  now = rdtsc()
  echo(repr(x))
  echo "Cycles: " & $(now - start)
  
  x = b
  start = rdtsc()
  sort1(x)
  now = rdtsc()
  echo(repr(x))
  echo "Cycles: " & $(now - start)
  
  x = a
  start = rdtsc()
  sort2(x)
  now = rdtsc()
  echo(repr(x))
  echo "Cycles: " & $(now - start)
  
  x = b
  start = rdtsc()
  sort2(x)
  now = rdtsc()
  echo(repr(x))
  echo "Cycles: " & $(now - start)

when isMainModule:
  testIt()


Recently I found

[http://stackoverflow.com/questions/2786899/fastest-sort-of-fixed-length-6-int-array](http://forum.nim-lang.org///stackoverflow.com/questions/2786899/fastest-sort-of-fixed-length-6-int-array)

That seems to be possible in C with only 26 clock cycles.

And indeed the Nim code version needs also only 26 cycles for a single array.

When I test with above code, I get these 26 cycles for the second value, and 
much more for the other 3:


[0, 1, 2, 3, 4, 5]
Cycles: 290
[0, 1, 2, 3, 4, 5]
Cycles: 26
[0, 1, 2, 3, 4, 5]
Cycles: 304
[0, 1, 2, 3, 4, 5]
Cycles: 212



Re: unescape \n \r etc in string

2016-12-15 Thread cblake
Perhaps you want strutils.escape? I.e.: 


import strutils
echo escape("\n\t")


Output is "\x0A\x09"

Its output is designed to be as portable an 'input' to 
de-escapers/escape-interpreters as possible. So you will see things like \x0A 
and \x09 instead of the \n \t because the former syntax is accepted by more 
string parsers.


unescape \n \r etc in string

2016-12-15 Thread Krux02
I have a macro with a string argument. The string is partially preserved, but 
all n from the string are now litterally n and t. so how do I unescape 
thesevalues to create a string object out of them? All my ideas right now feel 
like a lot of work with some uncertenty if I did everything correct, so I 
thought I might better ask, even though the implementation is propably trivial 
I do not think that the correct implementation is.