Function IsPrime(TestNumber As Long) As Boolean
Dim Count As Long
Dim HalfNumber As Long
HalfNumber = TestNumber / 2 + 1
For Count = 2 To HalfNumber
If (TestNumber Mod Count) = 0 Then
IsPrime = False
Exit Function
End If
Next
IsPrime = True
End Function
Private Sub Command1_Click()
Dim Number As Long
Dim MyTime As Variant
Dim Count As Long
Count = Val(Text1.Text)
MyTime = Timer
For Number = 1 To Count
IsPrime (Number)
Next Number
MsgBox (Timer - MyTime) & "seconds"
End Sub
Figure 2 IsPrime Ported to C
#include <STDIO.H>
#include <WINDOWS.H>
unsigned char IsPrime(unsigned long TestNumber);
unsigned long NextNumber, StartTime, CurrTime, NoPrime;
void main(int argc, char **argv)
{
StartTime = GetTickCount();
for(NextNumber = 1; NextNumber <= 50000; NextNumber++)
IsPrime(NextNumber);
CurrTime = (GetTickCount() - StartTime) / 1000;
printf("Time = %l seconds\n", CurrTime);
}
/* Tests for prime numbers */
unsigned char IsPrime(unsigned long TestNumber)
{
unsigned long count;
unsigned long HalfNumber = TestNumber / 2 + 1;
for(count = 2; count < HalfNumber; count++)
if(TestNumber % count == 0)
return FALSE;
return TRUE;
}
Figure 3 Code Execution Times
Code Type |
Elapsed time (in seconds) |
Percentage of p-code speed |
Within the Visual Basic environment |
134.63 |
79% |
Compiled p-code |
106.72 |
100% |
Compiled native code |
40.15 |
265% |
C program |
35.12 |
305% |
Figure 4 UserControl Properties
Property |
Description |
AccessKeys |
String containing all mnemonics for this control |
Alignable |
Determines whether the UserControl can be aligned in its container |
Ambient |
Returns the container object that has ambient property values |
CanGetFocus |
Determines whether the UserControl can have the focus |
ContainedControls |
Returns the set of controls contained within the UserControl |
ControlContainer |
Determines whether the UserControl can act as a container for other controls at design time |
DefaultCancel |
Determines whether the UserControl behaves like a button |
EditAtDesignTime |
Determines if the UserControl will have an Edit verb on the menu |
EventsFrozen |
Returns whether events on the control are frozen |
Extender |
Returns the container object that has extended properties or methods for the control |
ForwardFocus |
Determines whether the UserControl behaves like a label |
Hyperlink |
Returns a Hyperlink object used for browser-style navigation |
InvisibleAtRuntime |
Determines whether the UserControl is invisible at runtime |
ParentControls |
Returns the set of controls contained by the UserControl's parent |
PropertyPages |
Determines the names of property pages for the UserControl |
Public |
Determines whether the control is accessible to other clients |
ToolboxBitmap |
References a graphic that represents the UserControl in the toolbox |
Figure 5 UserControl Methods
Method |
Description |
AsyncRead |
Asynchronously read data from a URL |
CancelAsyncRead |
Cancel an asynchronous read |
CanPropertyChange |
Asks the container of the UserControl if a control property may be changed |
PropertyChanged |
Signals that a property of the UserControl changed |
Figure 6 UserControl Events
Event |
Description |
AccessKeyPress |
Occurs when the user hits a mnemonic that the UserControl exposes |
AmbientChanged |
Occurs when an ambient value was changed by the container of a UserControl |
AsyncReadComplete |
Occurs when the specified property is done loading |
EnterFocus, ExitFocus |
Occurs when the UserControl is UIActivated or UIDeactivated (tabbed into or tabbed away from) |
Show, Hide |
Occurs when the UserControl is shown/hidden. |
InitProperties |
Occurs the first time a UserControl is created |
ReadProperties |
Occurs when a UserControl is asked to read its data from a file |
WriteProperties |
Occurs when a UserControl is asked to write its data to a file |
Figure 7 Ambient Properties
Ambient Property |
Description |
DisplayAsDefault |
Tells you whether your control is the default button for the container. If your control is the default, then pressing the Enter key is equivalent to clicking on your control. Controls that are the default (DisplayAsDefault = True) should draw an extra-heavy border that identifies the default button to the user. |
DisplayName |
The name of the particular instance of the control. It should be used when raising errors at design time so the developer who receives the error can identify the control instance causing the error. |
ForeColor, BackColor, and Font |
Hints your control can use to make its appearance match that of the container. For example, in the InitProperties event fired whenever an instance of your control is first placed in a container, it is highly recommended to set your control's ForeColor, BackColor, and Font properties to the values supplied by the ambient properties. |
TextAlign |
Tells the control how the client site would like the control to arrange the text it displays. If the value of the property is 0, the control should obey "general" alignment principles: text aligned to left, numbers to right. A value of 1 means left alignment, 2 means center alignment, 3 means right alignment, and 4 means justification. |
LocaleID |
Lets the container tell the control in which locale the control should consider itself to be running. |
Palette |
Provides the current palette of the container. |
UserMode |
Allows an instance of your control to determine whether it is executing at design time (UserMode = False) or at runtime (UserMode = True). |