Was bored so create this:
Lape
Simba Code:var
t, PH, n:integer;
function GroupDigits(n: integer; token: String): String;
var
b: integer;
begin
Result := IntToStr(n);
b := length(Result) + 1;
if (b > 3) then
repeat
b := b - 3;
if (b > 1) then
insert(token, Result, b);
until (b < 3);
end;
procedure MarkTime(var TimeMarker: Integer);
begin
TimeMarker := GetSystemTime;
end;
function TimeFromMark(TimeMarker: Integer): Integer;
begin
Result := GetSystemTime - TimeMarker;
end;
function RandString(chars:integer; upperCase:Boolean):string;
var
charArray: Array[0..1] of TStringArray;
i:Integer;
begin
charArray[0] := ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9'];
charArray[1] := ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9'];
for i:=0 to chars do
begin
case upperCase of
True: result := result + charArray[Random(2)][Random(34)];
False: result := result + charArray[0][Random(34)];
end;
end;
end;
begin
n := 1000000;
MarkTime(t);
randString(n, False);
Writeln('finished in ' + ToStr(TimeFromMark(t)) + ' ms');
PH := Round(n * (3600.0 / (GetTimeRunning / 1000.0)));
Writeln('Characters per hour ' + ToStr(Groupdigits(PH, ',')));
end.
Code:finished in 203 ms Characters per hour 1,905,692,230
Java
Characters per hour is messed up for some reason but whatever
java Code:package random;
import java.util.Random;
public class randomString {
static String Letters, randL;
public static int random(int num) {
Random Number = new Random();
int r = Number.nextInt(num);
return r;
}
public static String randChar(int times, boolean useCase) {
String[][] chars = {
{ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8",
"9" },
{ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8",
"9" }, };
randL = "";
Letters = "";
for (int x=0; x<times; x++) {
if (useCase) {
Letters = randL + chars[random(2)][random(34)];
} else {
Letters = randL + chars[0][random(34)];
}
}
return Letters;
}
public static void main(String args[]) {
int n = 10000000;
long startTime = System.currentTimeMillis();
randChar(n, true);
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
double PH = Math.round((n * (3600/(System.currentTimeMillis()/1000.0))));
System.out.println("Finished in " + duration + " ms");
System.out.println("Characters per hour " + PH);
}
}Code:Finished in 203 ms Characters per hour 3.0
c++
Thank to Le Jingle:
c++ Code:#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <cstdio>
#include <ctime>
using namespace std;
const int amt = 1000000000; // 1 Billion
const char x[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
string s = "";
int main() {
double duration;
cout << "Testing String: " << &s << ", Elements: " << amt << endl;
clock_t start;
start = clock();
for (int i = 0; i < amt; i++)
s += x[rand() % 26];
duration = (clock() - start) / (double) CLOCKS_PER_SEC;
cout << "Time: " << duration << " seconds" << endl;
cin.get();
return 0;
}
Python
Thanks to warpie:python Code:from random import randint
from string import ascii_lowercase, digits, ascii_uppercase
from time import time
def intWithCommas(x):
if type(x) not in [type(0), type(0L)]:
raise TypeError("Parameter must be an integer.")
if x < 0:
return '-' + intWithCommas(-x)
result = ''
while x >= 1000:
x, r = divmod(x, 1000)
result = ",%03d%s" % (r, result)
return "%d%s" % (x, result)
def rand_string(times, allchars=False):
if allchars: chars = ascii_lowercase + ascii_uppercase + digits
else: chars = ascii_lowercase + digits
size = len(chars)-1
letters = []
for x in range(times):
letters.append(chars[randint(0,size)])
return letters #"".join(letters)
if __name__ == '__main__':
n = 10000000
t = time()
rand_string(n, False)
print 'finished in', str((time() - t)*1000) ,'ms'
PH = int(round(n * (3600.0 / (time()-t))))
print 'Characters per hour', intWithCommas(PH)