[racket-users] Access pattern variable in macro

2017-10-03 Thread 'Shakin Billy' via Racket Users
hello,

how do i access the pattern variable var at compile time (phase 1?)
additional to an answer, could you point me to the right part of the 
documentation? i really couldn't find it.

#lang racket
(define inp (open-input-string "12+34"))

(define-syntax (terminal stx)
  (syntax-case stx ()
((_ name rx)
 (if (pregexp? (syntax->datum #'rx))
 #'(define (name)
 (regexp-try-match rx inp))
 #'(error "Pregexp expected.")

(define-syntax (prod stx)
  (syntax-case stx ()
((_ prod-name (name var))
 (cond ((procedure? (syntax->datum #'var)) #'1)
   ((procedure? #'var) #'2)
   ((procedure? 'var) #'2)
   ((procedure? #''var) #'3)
   ((procedure? #''var) #'4)
   ((procedure? (syntax 'var)) #'5)
   ((pregexp? #'var) #'0)
   (else #'(raise-syntax-error 'PROD-ARGUMENTS "Pregexp oder 
Procedure expected."))

(terminal digit #px"\\d")
(prod term (op1 digit))

thanks for your help! :-)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Access pattern variable in macro

2017-10-03 Thread 'Shakin Billy' via Racket Users
Thanks a lot.

Am 03.10.2017 9:05 nachm. schrieb "Matthew Butterick" :

>
> > On Oct 3, 2017, at 10:31 AM, 'Shakin Billy' via Racket Users <
> racket-users@googlegroups.com> wrote:
> >
> > how do i access the pattern variable var at compile time (phase 1?)
> > additional to an answer, could you point me to the right part of the
> documentation? i really couldn't find it.
>
>
> `var` isn't a `procedure?` during phase 1. It's just a piece of syntax. So
> your `cond`, which is evaluated in phase 1, will always fail.
>
> But if you shift the `cond` to phase 0, by putting it inside the syntax
> template, it will work:
>
>
> #lang racket
> (require rackunit)
> (define inp (open-input-string "12+34"))
>
> (define-syntax (terminal stx)
>   (syntax-case stx ()
> ((_ name rx)
>  (if (pregexp? (syntax->datum #'rx))
>  #'(define (name)
>  (regexp-try-match rx inp))
>  #'(error "Pregexp expected.")
>
> (define-syntax (prod stx)
>   (syntax-case stx ()
> ((_ prod-name (name var))
>  #'(cond
>  ((procedure? var) 0)
>  (else (error 'PROD-ARGUMENTS "Pregexp oder Procedure
> expected."))
>
> (terminal digit #px"\\d")
> (check-equal? (prod term (op1 digit)) 0)
>
> (define not-a-procedure 42)
> (check-exn exn:fail? (λ () (prod term (op1 not-a-procedure

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] beating java (speed)

2017-07-01 Thread &#x27;Shakin Billy&#x27; via Racket Users
hi,

i've been working a little on project euler problem 501.
my first attempt is a burte forcce loop.4iw rote it in java und racket and 
found java to be faster by factor 3:

racket

#lang typed/racket
(require racket/unsafe/ops)
(require racket/fixnum)
(require racket/trace)
;(require typed/racket/performance-hint) 

(: acht-teiler (-> Fixnum Boolean)) 
(define (acht-teiler (n : Fixnum))
  (: loop (-> Fixnum Fixnum Fixnum Fixnum Boolean))
  (define (loop (i : Fixnum) (count : Fixnum) (quadrat : Fixnum) (steigung : 
Fixnum))
(cond ((unsafe-fx= quadrat n) #f)
  ((unsafe-fx> quadrat n) (unsafe-fx= count 3))
  ((unsafe-fx= (unsafe-fxmodulo n i) 0) (loop (unsafe-fx+ i 1) 
(unsafe-fx+ count 1) (unsafe-fx+ quadrat steigung) (unsafe-fx+ steigung 2)))
  (else (loop (unsafe-fx+ 1 i) count (unsafe-fx+ quadrat steigung) 
(unsafe-fx+ steigung 2)
  (loop 2 0 4 5))

(acht-teiler 36)

(: achter (-> Fixnum Fixnum))
(define (achter (n : Fixnum))
  (: loop (-> Fixnum Fixnum Fixnum))
  (define (loop (i : Fixnum) (count : Fixnum))
(cond ((unsafe-fx= i n) count)
  ((acht-teiler i) (loop (unsafe-fx+ i 1) (unsafe-fx+ count 1)))
  (else (loop (unsafe-fx+ i 1) count
  (loop 2 0))

(time (achter 100))
; for 100 = 24, 30, 40, 42, 54, 56, 66, 70, 78 and 88
; f(100) = 10, f(1000) = 180 and f(10e6) = 224427

for java:

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class EightDivs  implements Runnable{

public static Object lock = new Object();
public static long count = 0;

private long low;
private long high;

public EightDivs(long low, long high){
this.low = low;
this.high = high;
}

public boolean hasEightDivsExact(long a){
int divs = 0;
for(long i =1;i<=a;i++){
if(a % i == 0){
divs++;
}
}
return divs == 8;
}

public boolean hasEightDivs(long a){
int divs = 0;
for(long i = 2; i*i <= a && divs < 4;i++){
if(a % i == 0){
divs++;
if (i*i == a && divs >= 3){
return false;
}
}
}
return divs == 3;
}

public static void main(String[] args) throws InterruptedException {
ExecutorService ex = Executors.newFixedThreadPool(4);
long old = 1;
long start = System.currentTimeMillis();
//  for(long i = 0L; i<= 1000_000L;i+=100_00){
//  ex.execute(new EightDivs(old, i));
System.out.println(old +" low " + i + " high");
//  old = i;
//  }
//  ex.shutdown();
//  ex.awaitTermination(300, TimeUnit.SECONDS);
new EightDivs(1, 1000_000).run();
long end = System.currentTimeMillis();
System.out.println((end-start)/1000d+" Seconds");
//  new EightDivs(1, 1000).hasEightDivs(24);

System.out.println(count);
}

@Override
public void run() {
for(long i = low;ihttps://groups.google.com/d/optout.


[racket-users] Re: beating java (speed)

2017-07-01 Thread &#x27;Shakin Billy&#x27; via Racket Users
thx for the answers so far!

multithreading:


Am Samstag, 1. Juli 2017 15:07:37 UTC+2 schrieb Shakin Billy:
> hi,
> 
> i've been working a little on project euler problem 501.
> my first attempt is a burte forcce loop.4iw rote it in java und racket and 
> found java to be faster by factor 3:
> 
> racket
> 
> #lang typed/racket
> (require racket/unsafe/ops)
> (require racket/fixnum)
> (require racket/trace)
> ;(require typed/racket/performance-hint) 
> 
> (: acht-teiler (-> Fixnum Boolean)) 
> (define (acht-teiler (n : Fixnum))
>   (: loop (-> Fixnum Fixnum Fixnum Fixnum Boolean))
>   (define (loop (i : Fixnum) (count : Fixnum) (quadrat : Fixnum) (steigung : 
> Fixnum))
> (cond ((unsafe-fx= quadrat n) #f)
>   ((unsafe-fx> quadrat n) (unsafe-fx= count 3))
>   ((unsafe-fx= (unsafe-fxmodulo n i) 0) (loop (unsafe-fx+ i 1) 
> (unsafe-fx+ count 1) (unsafe-fx+ quadrat steigung) (unsafe-fx+ steigung 2)))
>   (else (loop (unsafe-fx+ 1 i) count (unsafe-fx+ quadrat steigung) 
> (unsafe-fx+ steigung 2)
>   (loop 2 0 4 5))
> 
> (acht-teiler 36)
> 
> (: achter (-> Fixnum Fixnum))
> (define (achter (n : Fixnum))
>   (: loop (-> Fixnum Fixnum Fixnum))
>   (define (loop (i : Fixnum) (count : Fixnum))
> (cond ((unsafe-fx= i n) count)
>   ((acht-teiler i) (loop (unsafe-fx+ i 1) (unsafe-fx+ count 1)))
>   (else (loop (unsafe-fx+ i 1) count
>   (loop 2 0))
> 
> (time (achter 100))
> ; for 100 = 24, 30, 40, 42, 54, 56, 66, 70, 78 and 88
> ; f(100) = 10, f(1000) = 180 and f(10e6) = 224427
> 
> for java:
> 
> import java.util.concurrent.Executor;
> import java.util.concurrent.ExecutorService;
> import java.util.concurrent.Executors;
> import java.util.concurrent.TimeUnit;
> 
> public class EightDivs  implements Runnable{
> 
>   public static Object lock = new Object();
>   public static long count = 0;
>   
>   private long low;
>   private long high;
>   
>   public EightDivs(long low, long high){
>   this.low = low;
>   this.high = high;
>   }
>   
>   public boolean hasEightDivsExact(long a){
>   int divs = 0;
>   for(long i =1;i<=a;i++){
>   if(a % i == 0){
>   divs++;
>   }
>   }
>   return divs == 8;
>   }
>   
>   public boolean hasEightDivs(long a){
>   int divs = 0;
>   for(long i = 2; i*i <= a && divs < 4;i++){
>   if(a % i == 0){
>   divs++;
>   if (i*i == a && divs >= 3){
>   return false;
>   }
>   }
>   }
>   return divs == 3;
>   }
>   
>   public static void main(String[] args) throws InterruptedException {
>   ExecutorService ex = Executors.newFixedThreadPool(4);
>   long old = 1;
>   long start = System.currentTimeMillis();
> //for(long i = 0L; i<= 1000_000L;i+=100_00){
> //ex.execute(new EightDivs(old, i));
>   System.out.println(old +" low " + i + " high");
> //old = i;
> //}
> //ex.shutdown();
> //ex.awaitTermination(300, TimeUnit.SECONDS);
>   new EightDivs(1, 1000_000).run();
>   long end = System.currentTimeMillis();
>   System.out.println((end-start)/1000d+" Seconds");
> //new EightDivs(1, 1000).hasEightDivs(24);
>   
>   System.out.println(count);
>   }
> 
>   @Override
>   public void run() {
>   for(long i = low;i   if(hasEightDivs(i)){
> //System.out.println(i);
>   synchronized (lock) {
>   count++;
>   }
>   }
>   }
>   System.out.println(low +" low " + high + " high done");
>   }
> 
> }
> 
> 
> --END CODE
> 
> java code runs in 4.5 seconds
> racket code takes 12.5 seconds to complete (in cli-mode)
> 
> i typed racket and used unsafe operations. some perfomance hints from the 
> guide don't seem to apply since i already tyyped racket. 
> the optimization coach hints to use define-inline but it seems to be 
> available only in non-typed-raket.
> 
> what can i do to speed things up (except better algorithms?)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Re: beating java (speed)

2017-07-01 Thread &#x27;Shakin Billy&#x27; via Racket Users
thx for the answers so far!

(multithreading)
the java version was intended to use mutlithreading, but i commented it out, so 
it's singe-thread vs single-thread.

(compilation)
i'm not sure this will speed things up since i don't measure the time using cli 
tools but built-in tools which run after the jit-compilation took place, but i 
will give it a try.

@ wargrey:
when i removed all the unsafe operations the typechecker yells at me like:
Type Checker: type mismatch
  expected: Fixnum
  given: Integer in: (+ i 1)

so i typed the fixnums all to Integer, but isn' this too vague for fast 
optimizations?

on my computer the runtime prolonged to 50 seconds without unsafe 
operations(typing to integer)

i still don' wanna believe java is faster/better!

racket for the winner :)

Am Samstag, 1. Juli 2017 15:07:37 UTC+2 schrieb Shakin Billy:
> hi,
> 
> i've been working a little on project euler problem 501.
> my first attempt is a burte forcce loop.4iw rote it in java und racket and 
> found java to be faster by factor 3:
> 
> racket
> 
> #lang typed/racket
> (require racket/unsafe/ops)
> (require racket/fixnum)
> (require racket/trace)
> ;(require typed/racket/performance-hint) 
> 
> (: acht-teiler (-> Fixnum Boolean)) 
> (define (acht-teiler (n : Fixnum))
>   (: loop (-> Fixnum Fixnum Fixnum Fixnum Boolean))
>   (define (loop (i : Fixnum) (count : Fixnum) (quadrat : Fixnum) (steigung : 
> Fixnum))
> (cond ((unsafe-fx= quadrat n) #f)
>   ((unsafe-fx> quadrat n) (unsafe-fx= count 3))
>   ((unsafe-fx= (unsafe-fxmodulo n i) 0) (loop (unsafe-fx+ i 1) 
> (unsafe-fx+ count 1) (unsafe-fx+ quadrat steigung) (unsafe-fx+ steigung 2)))
>   (else (loop (unsafe-fx+ 1 i) count (unsafe-fx+ quadrat steigung) 
> (unsafe-fx+ steigung 2)
>   (loop 2 0 4 5))
> 
> (acht-teiler 36)
> 
> (: achter (-> Fixnum Fixnum))
> (define (achter (n : Fixnum))
>   (: loop (-> Fixnum Fixnum Fixnum))
>   (define (loop (i : Fixnum) (count : Fixnum))
> (cond ((unsafe-fx= i n) count)
>   ((acht-teiler i) (loop (unsafe-fx+ i 1) (unsafe-fx+ count 1)))
>   (else (loop (unsafe-fx+ i 1) count
>   (loop 2 0))
> 
> (time (achter 100))
> ; for 100 = 24, 30, 40, 42, 54, 56, 66, 70, 78 and 88
> ; f(100) = 10, f(1000) = 180 and f(10e6) = 224427
> 
> for java:
> 
> import java.util.concurrent.Executor;
> import java.util.concurrent.ExecutorService;
> import java.util.concurrent.Executors;
> import java.util.concurrent.TimeUnit;
> 
> public class EightDivs  implements Runnable{
> 
>   public static Object lock = new Object();
>   public static long count = 0;
>   
>   private long low;
>   private long high;
>   
>   public EightDivs(long low, long high){
>   this.low = low;
>   this.high = high;
>   }
>   
>   public boolean hasEightDivsExact(long a){
>   int divs = 0;
>   for(long i =1;i<=a;i++){
>   if(a % i == 0){
>   divs++;
>   }
>   }
>   return divs == 8;
>   }
>   
>   public boolean hasEightDivs(long a){
>   int divs = 0;
>   for(long i = 2; i*i <= a && divs < 4;i++){
>   if(a % i == 0){
>   divs++;
>   if (i*i == a && divs >= 3){
>   return false;
>   }
>   }
>   }
>   return divs == 3;
>   }
>   
>   public static void main(String[] args) throws InterruptedException {
>   ExecutorService ex = Executors.newFixedThreadPool(4);
>   long old = 1;
>   long start = System.currentTimeMillis();
> //for(long i = 0L; i<= 1000_000L;i+=100_00){
> //ex.execute(new EightDivs(old, i));
>   System.out.println(old +" low " + i + " high");
> //old = i;
> //}
> //ex.shutdown();
> //ex.awaitTermination(300, TimeUnit.SECONDS);
>   new EightDivs(1, 1000_000).run();
>   long end = System.currentTimeMillis();
>   System.out.println((end-start)/1000d+" Seconds");
> //new EightDivs(1, 1000).hasEightDivs(24);
>   
>   System.out.println(count);
>   }
> 
>   @Override
>   public void run() {
>   for(long i = low;i   if(hasEightDivs(i)){
> //System.out.println(i);
>   synchronized (lock) {
>   count++;
>   }
>   }
>   }
>   System.out.println(low +" low " + high + " high done");
>   }
> 
> }
> 
> 
> --END CODE
> 
> java code runs in 4.5 seconds
> racket code takes 12.5 seconds to complet

[racket-users] Re: beating java (speed)

2017-07-02 Thread &#x27;Shakin Billy&#x27; via Racket Users
@ matthias
i'm aware that java got more manpower, but since this is the first time i 
compare racket and java in terms of speed i didn't know what to expect. I know 
there are many loop optimizations but it doesn't seem to me many of them apply 
here since it's just two nested loops without array access mangeled(no 
reoerdering of access, no unrolling). but it's fast enough for me anyway ! :-)

@jens axel
on my mashine it took 320 seconds to finish

@vincent
i'll try  inlining soon

Am Samstag, 1. Juli 2017 15:07:37 UTC+2 schrieb Shakin Billy:
> hi,
> 
> i've been working a little on project euler problem 501.
> my first attempt is a burte forcce loop.4iw rote it in java und racket and 
> found java to be faster by factor 3:
> 
> racket
> 
> #lang typed/racket
> (require racket/unsafe/ops)
> (require racket/fixnum)
> (require racket/trace)
> ;(require typed/racket/performance-hint) 
> 
> (: acht-teiler (-> Fixnum Boolean)) 
> (define (acht-teiler (n : Fixnum))
>   (: loop (-> Fixnum Fixnum Fixnum Fixnum Boolean))
>   (define (loop (i : Fixnum) (count : Fixnum) (quadrat : Fixnum) (steigung : 
> Fixnum))
> (cond ((unsafe-fx= quadrat n) #f)
>   ((unsafe-fx> quadrat n) (unsafe-fx= count 3))
>   ((unsafe-fx= (unsafe-fxmodulo n i) 0) (loop (unsafe-fx+ i 1) 
> (unsafe-fx+ count 1) (unsafe-fx+ quadrat steigung) (unsafe-fx+ steigung 2)))
>   (else (loop (unsafe-fx+ 1 i) count (unsafe-fx+ quadrat steigung) 
> (unsafe-fx+ steigung 2)
>   (loop 2 0 4 5))
> 
> (acht-teiler 36)
> 
> (: achter (-> Fixnum Fixnum))
> (define (achter (n : Fixnum))
>   (: loop (-> Fixnum Fixnum Fixnum))
>   (define (loop (i : Fixnum) (count : Fixnum))
> (cond ((unsafe-fx= i n) count)
>   ((acht-teiler i) (loop (unsafe-fx+ i 1) (unsafe-fx+ count 1)))
>   (else (loop (unsafe-fx+ i 1) count
>   (loop 2 0))
> 
> (time (achter 100))
> ; for 100 = 24, 30, 40, 42, 54, 56, 66, 70, 78 and 88
> ; f(100) = 10, f(1000) = 180 and f(10e6) = 224427
> 
> for java:
> 
> import java.util.concurrent.Executor;
> import java.util.concurrent.ExecutorService;
> import java.util.concurrent.Executors;
> import java.util.concurrent.TimeUnit;
> 
> public class EightDivs  implements Runnable{
> 
>   public static Object lock = new Object();
>   public static long count = 0;
>   
>   private long low;
>   private long high;
>   
>   public EightDivs(long low, long high){
>   this.low = low;
>   this.high = high;
>   }
>   
>   public boolean hasEightDivsExact(long a){
>   int divs = 0;
>   for(long i =1;i<=a;i++){
>   if(a % i == 0){
>   divs++;
>   }
>   }
>   return divs == 8;
>   }
>   
>   public boolean hasEightDivs(long a){
>   int divs = 0;
>   for(long i = 2; i*i <= a && divs < 4;i++){
>   if(a % i == 0){
>   divs++;
>   if (i*i == a && divs >= 3){
>   return false;
>   }
>   }
>   }
>   return divs == 3;
>   }
>   
>   public static void main(String[] args) throws InterruptedException {
>   ExecutorService ex = Executors.newFixedThreadPool(4);
>   long old = 1;
>   long start = System.currentTimeMillis();
> //for(long i = 0L; i<= 1000_000L;i+=100_00){
> //ex.execute(new EightDivs(old, i));
>   System.out.println(old +" low " + i + " high");
> //old = i;
> //}
> //ex.shutdown();
> //ex.awaitTermination(300, TimeUnit.SECONDS);
>   new EightDivs(1, 1000_000).run();
>   long end = System.currentTimeMillis();
>   System.out.println((end-start)/1000d+" Seconds");
> //new EightDivs(1, 1000).hasEightDivs(24);
>   
>   System.out.println(count);
>   }
> 
>   @Override
>   public void run() {
>   for(long i = low;i   if(hasEightDivs(i)){
> //System.out.println(i);
>   synchronized (lock) {
>   count++;
>   }
>   }
>   }
>   System.out.println(low +" low " + high + " high done");
>   }
> 
> }
> 
> 
> --END CODE
> 
> java code runs in 4.5 seconds
> racket code takes 12.5 seconds to complete (in cli-mode)
> 
> i typed racket and used unsafe operations. some perfomance hints from the 
> guide don't seem to apply since i already tyyped racket. 
> the optimization coach hints to use define-inline but it seems to be 
> available only in non-typed-raket.
> 
> what can i do to speed things

[racket-users] Re: beating java (speed)

2017-07-04 Thread &#x27;Shakin Billy&#x27; via Racket Users
i couldn't make the inlining via define-syntax-rule work in typed racket. in 
normal racket define-inline did'nt speed things up much but changing the code 
of "acht-teiler" to:
CODE--
(define-syntax-rule (acht-teiler n)
  (let loop ((i 2) (count 0) (quadrat 4) (steigung 5))
(cond ((= quadrat n) #f)
  ((> quadrat n) (unsafe-fx= count 3))
  ((unsafe-fx= (unsafe-fxmodulo n i) 0) (loop (unsafe-fx+ i 1) 
(unsafe-fx+ count 1) (unsafe-fx+ quadrat steigung) (unsafe-fx+ steigung 2)))
  (else (loop (unsafe-fx+ 1 i) count (unsafe-fx+ quadrat steigung) 
(unsafe-fx+ steigung 2)
  (loop 2 0 4 5)
  )
--CODE
that is: no inner define but  a let brought the same speed up as using 
typed-racket

is let in general faster than define?


@ gneuner2
jitting the function with a previous call yields only slightly better results 
(on my mashine in the range of milliseconds)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.