Hiển thị các bài đăng có nhãn D_language. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn D_language. Hiển thị tất cả bài đăng

Thứ Ba, 15 tháng 11, 2016

Using std.datetime.StopWatch to time sections of D code

By Vasudev Ram




Accuracy_and_precision image attribution

This D program shows how you can use the std.datetime module from Phobos, D's standard library, to time the execution of sections of D code. I use the StopWatch class from that module. I print the elapsed time in both milliseconds and in microseconds in this program. (Depending on the functions used from this module and the OS and hardware platform, it may also be possible to measure time to an accuracy of hecto-nanoseconds.

The D documentation for the StopWatch class says:

[ StopWatch measures time as precisely as possible.
This class uses a high-performance counter. On Windows systems, it uses QueryPerformanceCounter, and on Posix systems, it uses clock_gettime if available, and gettimeofday otherwise.
But the precision of StopWatch differs from system to system. It is impossible for it to be the same from system to system since the precision of the system clock varies from system to system, and other system-dependent and situation-dependent stuff (such as the overhead of a context switch between threads) can also affect StopWatch's accuracy. ]

Here are a couple of links about the difference between accuracy and precision:

On Wikipedia: Accuracy and Precision

On ncse.edu: Accuracy and Precision

The program for timing D code is below, in the file test_stopwatch.d. It is straightforward, so should be somewhat possible for even people unfamiliar with D to understand. It counts from 1 to 1 billion and measures the time taken for that. Note the use of the more human-readable constant 1_000_000_000, using underscores to separate groups of digits, like Perl can. A nice small D feature. Wish more languages had it.
/*********************************************************************
File: test_stopwatch.d
Author: Vasudev Ram
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram
*********************************************************************/

import std.stdio;
import std.datetime;

void foo() {
// limit is set to 1000 million which is 1 billion.
auto limit = 1_000_000_000;
writeln("Counting from 1 to ", limit);
foreach (i; 1 .. limit) {
// This is a do-nothing loop since we are only
// interested in counting from 1 to limit.
}
}

int main(string[] args) {
// Declare a variable sw of type StopWatch.
StopWatch sw;
// Start the stopwatch.
sw.start();
// Call function foo() which takes up some time
// by counting up to some large number.
foo();
// Stop the stopwatch.
sw.stop();
// Get the elapsed time in microseconds.
auto usecs = sw.peek().usecs;
// Get the elapsed time in milliseconds.
auto msecs = sw.peek().msecs;
// Get the elapsed time in seconds.
auto seconds = sw.peek().seconds;
writeln("Time in usecs: ", usecs);
writeln("Time in msecs: ", msecs);
writeln("Time in seconds: ", seconds);
return 0;
}
Here is the comand to compile the program.
$ dmd test_stopwatch.d
And here is the output of 3 runs:
$ test_stopwatch
Counting from 1 to 1000000000
Time in usecs: 3136388
Time in msecs: 3136
Time in seconds: 3

$ test_stopwatch
Counting from 1 to 1000000000
Time in usecs: 4231483
Time in msecs: 4231
Time in seconds: 4

$ test_stopwatch
Counting from 1 to 1000000000
Time in usecs: 4603806
Time in msecs: 4603
Time in seconds: 4

- Vasudev Ram - Online Python training and consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

Subscribe to my blog by email

My ActiveState recipes

Managed WordPress Hosting by FlyWheel



Thứ Bảy, 14 tháng 5, 2016

[DLang]: A simple file download utility in D

By Vasudev Ram



Download image attribution

Hi readers,

Here is another in my recently started series of posts about D (DLang / the D language).

This is a simple download utility written in D, to download a file from a URL.

It makes use of a high-level function called download (sic) in the std.net.curl module, so the code is very simple. But it only handles basic cases; e.g. no authentication or HTTPS support.
/*
File: download.d
Version: 0.1
Purpose: A simple program to download a file from a given URL
to a given local file. Only handles simple cases. Does not
support authentication, HTTPS or other features.
Author: Vasudev Ram - https://vasudevram.github.io
Copyright 2016 Vasudev Ram
*/

import std.stdio;
import std.net.curl;

void usage(string program)
{
writeln("Usage: ", program, " URL file");
writeln("Downloads the contents of URL to file.");
}

int main(string[] args)
{
if (args.length != 3)
{
usage(args[0]);
return 1;
}
try
{
writeln("Trying to download (URL) ", args[1], " to (file) ", args[2]);
download(args[1], args[2]);
writeln("Item at URL ", args[1], " downloaded to file ", args[2]);
return 0;
}
catch(Exception e)
{
writeln("Error: ", e.msg);
debug(1) writeln("Exception info:\n", e);
return 1;
}
}
A makefile to build it in both debug and release versions:
$ type Makefile

release: download.d
dmd -ofdownload.exe download.d

debug: download.d
dmd -debug -ofdownload.exe download.d
To build the release version:
make release
To build the debug version:
make debug
A test run: download the current HN home page:
download news.ycombinator.com nyc.html
Output is in nyc.html; see screenshot below:


The line:
debug(1) writeln("Exception info:\n", e);
gets compiled into the binary / EXE only if you build the debug version.
That line prints a bit more information about the exception than the release version does.

You can also read a few posts about downloading using Python tools.

- Enjoy.

- Vasudev Ram - Online Python training and consulting

Signup to hear about my new courses and products.

My Python posts     Subscribe to my blog by email

My ActiveState recipes



Thứ Năm, 12 tháng 5, 2016

Getting CPU info with D (the D language)

By Vasudev Ram



I have been using the D programming language for some time.

From the D home page:

[ D is a systems programming language with C-like syntax and static typing. It combines efficiency, control and modeling power with safety and programmer productivity. ]

[ Some information about D:

- D Language home page
- D Overview
- D on Wikipedia ]

Here is a D program, cpu_info.d, that gets some information about the CPU of your machine.
// cpu_info.d
// Author: Vasudev Ram - https://vasudevram.github.io
// http://jugad2.blogspot.com
import std.stdio;
import core.cpuid;
void main()
{
writeln("processor: ", processor());
writeln("vendor: ", vendor());
writeln("hyperThreading: ", hyperThreading());
writeln("threadsPerCPU: ", threadsPerCPU());
writeln("coresPerCPU: ", coresPerCPU());
}
The program can be compiled and run with:
$ dmd cpu_info.d
dmd is the D compiler (the name stands for Digital Mars D). It creates cpu_info.exe which I can then run:
$ cpu_info
processor: Intel(R) Core(TM) i3-2328M CPU @ 2.20GHz
vendor: GenuineIntel
hyperThreading: true
threadsPerCPU: 4
coresPerCPU: 2
- Vasudev Ram - Online Python training and consulting

Signup to hear about my new courses and products.

My Python posts     Subscribe to my blog by email

My ActiveState recipes