Ignore:
Timestamp:
Apr 12, 2017, 9:36:33 PM (7 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
50b7e8c, d9dd3d1
Parents:
ff178ee
Message:

Add printing code to benchmark

Location:
doc/generic_types/evaluation
Files:
6 added
9 edited

Legend:

Unmodified
Added
Removed
  • doc/generic_types/evaluation/Makefile

    rff178ee r0d10090  
    11CFA = my-cfa
    22DEPFLAGS = -MMD -MP
    3 CFLAGS = -O2 -flto
     3CFLAGS = -O2
     4ifdef N
     5CFLAGS += -DN=$(N)
     6endif
    47CXXFLAGS = $(CFLAGS) --std=c++14
    58
     
    2528        $(COMPILE.cfa) $(OUTPUT_OPTION) -c $<
    2629
    27 COBJS = c-stack.o c-pair.o
     30COBJS = c-stack.o c-pair.o c-print.o
    2831CPPOBJS =
    2932CPPVOBJS = cpp-vstack.o
    30 CFAOBJS = cfa-stack.o cfa-pair.o
     33CFAOBJS = cfa-stack.o cfa-pair.o cfa-print.o
    3134
    3235c-bench: c-bench.c c-bench.d $(COBJS)
     
    6568        @echo '## Cforall ##'
    6669        @/usr/bin/time -f 'max_memory:\t %M kilobytes' ./cfa-bench
    67         @printf 'source_size:\t%8d lines\n' `cat cfa-bench.c bench.h cfa-stack.h cfa-stack.c | wc -l`
     70        @printf 'source_size:\t%8d lines\n' `cat cfa-bench.c bench.h cfa-stack.h cfa-stack.c cfa-print.h cfa-print.c | wc -l`
    6871        @printf 'binary_size:\t%8d bytes\n' `stat -c %s cfa-bench`
    6972
     
    7275        @echo '## C++ ##'
    7376        @/usr/bin/time -f 'max_memory:\t %M kilobytes' ./cpp-bench
    74         @printf 'source_size:\t%8d lines\n' `cat cpp-bench.cpp bench.hpp cpp-stack.hpp | wc -l`
     77        @printf 'source_size:\t%8d lines\n' `cat cpp-bench.cpp bench.hpp cpp-stack.hpp cpp-print.hpp | wc -l`
    7578        @printf 'binary_size:\t%8d bytes\n' `stat -c %s cpp-bench`
    7679
  • doc/generic_types/evaluation/bench.h

    rff178ee r0d10090  
    44#include <time.h>
    55
    6 #define N 50000000
     6#ifndef N
     7#define N 40000000
     8#endif
    79
    810long ms_between(clock_t start, clock_t end) {
  • doc/generic_types/evaluation/bench.hpp

    rff178ee r0d10090  
    55#include <time.h>
    66
    7 static const int N = 50000000;
     7#ifndef N
     8static const int N = 40000000;
     9#endif
    810
    911long ms_between(clock_t start, clock_t end) {
  • doc/generic_types/evaluation/c-bench.c

    rff178ee r0d10090  
     1#include <stdio.h>
    12#include <stdlib.h>
    23#include "bench.h"
    34#include "c-pair.h"
    45#include "c-stack.h"
     6#include "c-print.h"
    57
    68_Bool* new_bool( _Bool b ) {
     
    8991        )
    9092        free_pair_bool_char( max2 );
     93
     94        FILE* out = fopen("c-out.txt", "w");
     95        REPEAT_TIMED( "print_int",
     96                print( out, "dsds", rand(), ":", rand(), "\n" );
     97        )
     98
     99        REPEAT_TIMED( "print_pair",
     100                struct pair p1 = ((struct pair){ new_bool(rand() & 0x1), new_char(rand() & 0x7F) });
     101                struct pair p2 = ((struct pair){ new_bool(rand() & 0x1), new_char(rand() & 0x7F) });
     102                print( out, "pbcspbcs", p1, ":", p2, "\n" );
     103                free(p1.first); free(p1.second);
     104                free(p2.first); free(p2.second);
     105        )
     106        fclose(out);
    91107}
  • doc/generic_types/evaluation/cfa-bench.c

    rff178ee r0d10090  
    11#include <stdlib>
    22#include <stdlib.h>
     3#include <stdio.h>
    34#include "pair"
    45#include "bench.h"
    56#include "cfa-stack.h"
     7#include "cfa-print.h"
    68
    79int main(int argc, char** argv) {
     
    4547                max2 = max( max2, pop( &t2 ) );
    4648        )
     49
     50        FILE* out = fopen("cfa-out.txt", "w");
     51        REPEAT_TIMED( "print_int",
     52                print( out, rand(), ":", rand(), "\n" );
     53        )
     54
     55        REPEAT_TIMED( "print_pair",
     56                print( out, (pair(_Bool, char)){ rand() & 0x1, rand() & 0x7F }, ":",
     57                                (pair(_Bool, char)){ rand() & 0x1, rand() & 0x7F }, "\n" );
     58        )
     59        fclose(out);
    4760}
  • doc/generic_types/evaluation/cpp-bench.cpp

    rff178ee r0d10090  
    11#include <algorithm>
     2#include <fstream>
    23#include <stdlib.h>
    34#include <utility>
    45#include "bench.hpp"
    56#include "cpp-stack.hpp"
     7#include "cpp-print.hpp"
    68
    79int main(int argc, char** argv) {
     
    4547                max2 = std::max( max2, t2.pop() );
    4648        )
     49
     50        std::ofstream out{"cpp-out.txt"};
     51        REPEAT_TIMED( "print_int",
     52                print( out, rand(), ":", rand(), "\n" );
     53        )
     54
     55        REPEAT_TIMED( "print_pair",
     56                print( out, std::pair<bool, char>{ rand() & 0x1, rand() & 0x7F }, ":",
     57                                std::pair<bool, char>{ rand() & 0x1, rand() & 0x7F }, "\n" );
     58        )
    4759}
  • doc/generic_types/evaluation/cpp-stack.hpp

    rff178ee r0d10090  
    3333                        delete crnt;
    3434                }
     35                head = nullptr;
    3536        }
    3637
  • doc/generic_types/evaluation/cpp-vbench.cpp

    rff178ee r0d10090  
    11#include <algorithm>
     2#include <fstream>
    23#include <stdlib.h>
    34#include "bench.hpp"
    45#include "cpp-vstack.hpp"
     6#include "cpp-vprint.hpp"
    57#include "object.hpp"
    68
     
    4547                std::make_unique<character>('\0') );
    4648        REPEAT_TIMED( "pop_bool_char",
    47                 std::unique_ptr<object> x = t2.pop();
    48                 if ( x->as<pair>() > *max2 ) { max2.reset( static_cast<pair*>(x.release()) ); }
     49                std::unique_ptr<pair> x = as_ptr<pair>( t2.pop() );
     50                if ( *x > *max2 ) { max2 = std::move(x); }
     51        )
     52
     53        std::ofstream out{"cpp-vout.txt"};
     54        REPEAT_TIMED( "print_int",
     55                print( out, integer{rand()}, c_string{":"}, integer{rand()}, c_string{"\n"} );
     56        )
     57
     58        REPEAT_TIMED( "print_pair",
     59                print( out, pair{ std::make_unique<boolean>( rand() & 0x1 ),
     60                        std::make_unique<character>( rand() & 0x7F ) }, c_string{":"},
     61                        pair{ std::make_unique<boolean>( rand() & 0x1 ),
     62                        std::make_unique<character>( rand() & 0x7F ) }, c_string{"\n"} );
    4963        )
    5064}
  • doc/generic_types/evaluation/object.hpp

    rff178ee r0d10090  
    11#pragma once
    22
     3#include <cstddef>
    34#include <exception>
     5#include <iomanip>
    46#include <memory>
     7#include <ostream>
    58#include <string>
    69#include <typeinfo>
     
    2427class object {
    2528public:
    26         std::type_index get_class() const { return { typeid(*this) }; }
     29        std::type_index get_class() const { return { this ? typeid(*this) : typeid(std::nullptr_t) }; }
    2730
    2831        template<typename T>
    2932        T& as() {
    30                 std::type_index from = get_class(), to = class_of<T>();
    31                 if ( from != to ) throw bad_cast{ from, to };
    32                 return reinterpret_cast<T&>(*this);
     33                T* p = dynamic_cast<T*>(this);
     34                if ( !p ) throw bad_cast{ get_class(), class_of<T>() };
     35                return *p;
    3336        }
    3437
    3538        template<typename T>
    3639        const T& as() const {
    37                 std::type_index from = get_class(), to = class_of<T>();
    38                 if ( from != to ) throw bad_cast{ from, to };
    39                 return reinterpret_cast<const T&>(*this);
     40                const T* p = dynamic_cast<const T*>(this);
     41                if ( !p ) throw bad_cast{ get_class(), class_of<T>() };
     42                return *p;
    4043        }
    4144
     
    4952};
    5053
    51 template<typename T>
    52 T* as_subclass_of( object* o ) {
    53         T* r = dynamic_cast<T*>( o );
    54         if ( r == nullptr ) throw bad_cast{ o->get_class(), class_of<T>() };
    55         return r;
     54template<typename To, typename From>
     55std::unique_ptr<To> as_ptr( std::unique_ptr<From>&& p ) {
     56        return std::unique_ptr<To>{ &p.release()->template as<To>() };
    5657}
    5758
    58 template<typename T>
    59 const T* as_subclass_of( const object* o ) {
    60         const T* r = dynamic_cast<const T*>( o );
    61         if ( r == nullptr ) throw bad_cast{ o->get_class(), class_of<T>() };
    62         return r;
    63 }
    64 
    65 class ordered : public object {
     59class ordered : public virtual object {
    6660public:
    6761        virtual int cmp(const ordered&) const = 0;
     
    8074};
    8175
    82 class boolean : public ordered {
    83 private:
     76class printable : public virtual object {
     77public:
     78        virtual void print(std::ostream&) const = 0;
     79};
     80
     81class boolean : public ordered, public printable {
    8482        bool x;
    8583
     
    9896        }
    9997
    100         object& operator= (const object& that) override {
    101                 std::type_index from = that.get_class(), to = get_class();
    102                 if ( from != to ) throw bad_cast{ from, to };
    103                 return *this = static_cast<const boolean&>(that);
    104         }
     98        object& operator= (const object& that) override { return *this = that.as<boolean>(); }
    10599
    106100        ~boolean() override = default;
     
    108102        int cmp(const boolean& that) const { return x == that.x ? 0 : x == false ? -1 : 1; }
    109103
    110         // bool operator< (const boolean& that) const { return x < that.x; }
    111 
    112         // bool operator== (const boolean& that) const { return x == that.x; }
    113 
    114         int cmp(const ordered& that) const override {
    115                 std::type_index from = that.get_class(), to = get_class();
    116                 if ( from != to ) throw bad_cast{ from, to };
    117                 return cmp( static_cast<const boolean&>(that) );
    118         }
    119 };
    120 
    121 class character : public ordered {
    122 private:
     104        int cmp(const ordered& that) const override { return cmp( that.as<boolean>() ); }
     105
     106        void print(std::ostream& out) const override { out << (x ? "true" : "false"); }
     107};
     108
     109class character : public ordered, public printable {
    123110        char x;
    124111
     
    137124        }
    138125
    139         object& operator= (const object& that) override {
    140                 std::type_index from = that.get_class(), to = get_class();
    141                 if ( from != to ) throw bad_cast{ from, to };
    142                 return *this = static_cast<const character&>(that);
    143         }
     126        object& operator= (const object& that) override { return *this = that.as<character>(); }
    144127
    145128        ~character() override = default;
     
    147130        int cmp(const character& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
    148131
    149         // bool operator< (const character& that) const { return x < that.x; }
    150 
    151         // bool operator== (const character& that) const { return x == that.x; }
    152 
    153         int cmp(const ordered& that) const override {
    154                 std::type_index from = that.get_class(), to = get_class();
    155                 if ( from != to ) throw bad_cast{ from, to };
    156                 return cmp( static_cast<const character&>(that) );
    157         }
    158 };
    159 
    160 class integer : public ordered {
    161 private:
     132        int cmp(const ordered& that) const override { return cmp( that.as<character>() ); }
     133
     134        void print(std::ostream& out) const override {
     135                if ( 0x20 <= x && x <= 0x7E ) { out << "'" << x << "'"; }
     136                else { out << "'\\" << std::hex << (unsigned int)x << std::setbase(0) << "'"; }
     137        }
     138};
     139
     140class integer : public ordered, public printable {
    162141        int x;
    163142
     
    176155        }
    177156
    178         object& operator= (const object& that) override {
    179                 std::type_index from = that.get_class(), to = get_class();
    180                 if ( from != to ) throw bad_cast{ from, to };
    181                 return *this = static_cast<const integer&>(that);
    182         }
     157        object& operator= (const object& that) override { return *this = that.as<integer>(); }
    183158
    184159        ~integer() override = default;
     
    186161        int cmp(const integer& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
    187162
    188         // bool operator< (const integer& that) const { return x < that.x; }
    189 
    190         // bool operator== (const integer& that) const { return x == that.x; }
    191 
    192         int cmp(const ordered& that) const override {
    193                 std::type_index from = that.get_class(), to = get_class();
    194                 if ( from != to ) throw bad_cast{ from, to };
    195                 return cmp( static_cast<const integer&>(that) );
    196         }
    197 };
    198 
    199 class pair : public ordered {
    200 private:
     163        int cmp(const ordered& that) const override { return cmp( that.as<integer>() ); }
     164
     165        void print(std::ostream& out) const override { out << x; }
     166};
     167
     168class c_string : public printable {
     169        static constexpr const char* empty = "";
     170        const char* s;
     171public:
     172        c_string() : s(empty) {}
     173
     174        c_string(const char* s) : s(s) {}
     175
     176        std::unique_ptr<object> new_inst() const override { return std::make_unique<c_string>(); }
     177
     178        std::unique_ptr<object> new_copy() const override { return std::make_unique<c_string>(s); }
     179
     180        c_string& operator= (const c_string& that) {
     181                s = that.s;
     182                return *this;
     183        }
     184
     185        object& operator= (const object& that) override { return *this = that.as<c_string>(); }
     186
     187        ~c_string() override = default;
     188
     189        void print(std::ostream& out) const override { out << s; }
     190};
     191
     192class pair : public ordered, public printable {
    201193        std::unique_ptr<object> x;
    202194        std::unique_ptr<object> y;
     
    220212        }
    221213
    222         object& operator= (const object& that) override {
    223                 std::type_index from = that.get_class(), to = get_class();
    224                 if ( from != to ) throw bad_cast{ from, to };
    225                 return *this = static_cast<const pair&>(that);
    226         }
     214        object& operator= (const object& that) override { return *this = that.as<pair>(); }
    227215
    228216        ~pair() override = default;
    229217
    230218        int cmp(const pair& that) const {
    231                 const ordered* a = as_subclass_of<ordered>( x.get() );
    232                 const ordered* b = as_subclass_of<ordered>( that.x.get() );
    233                 int c = a->cmp( *b );
     219                int c = x->as<ordered>().cmp( that.x->as<ordered>() );
    234220                if ( c != 0 ) return c;
    235                 a = as_subclass_of<ordered>( y.get() );
    236                 b = as_subclass_of<ordered>( that.y.get() );
    237                 return a->cmp( *b );
    238         }
    239 
    240         // bool operator< (const pair& that) const { return cmp(that) < 0; }
    241 
    242         // bool operator== ( const pair& that) const { return cmp(that) == 0; }
    243 
    244         int cmp(const ordered& that) const override {
    245                 std::type_index from = that.get_class(), to = get_class();
    246                 if ( from != to ) throw bad_cast{ from, to };
    247                 return cmp( static_cast<const pair&>(that) );
    248         }
    249 };
     221                return y->as<ordered>().cmp( that.y->as<ordered>() );
     222        }
     223
     224        int cmp(const ordered& that) const override { return cmp( that.as<pair>() ); }
     225
     226        void print(std::ostream& out) const override {
     227                out << "[";
     228                x->as<printable>().print(out);
     229                out << ", ";
     230                y->as<printable>().print(out);
     231                out << "]";
     232        }
     233};
Note: See TracChangeset for help on using the changeset viewer.